Simplify how paths are pushed

Previously we were setting up Nix build hook to make sure all paths are
synced to cachix.

This had a few flaws:

- nix-daemon had to be restarted, resulting into a few seconds extra
  setup per each build
- spurious error on macos due to nix-daemon socket not ready in time
- significant overhead since syncing to cachix was synchronous for each
  derivation built

Now we just look for new store paths and push those after nix-build.
This commit is contained in:
Domen Kožar 2020-02-24 14:14:49 +01:00
commit 7bea2cc0c1
No known key found for this signature in database
GPG key ID: C2FFBCAFD2C24246
3 changed files with 45 additions and 90 deletions

View file

@ -14,7 +14,7 @@ jobs:
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
- uses: cachix/install-nix-action@v6
- uses: cachix/install-nix-action@v7
- name: Test public cache
uses: ./
with:

View file

@ -18,25 +18,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const os_1 = require("os");
const strings_1 = require("./strings");
function home() {
if (os_1.type() == "Darwin") {
return "/Users/runner";
}
else {
return "/home/runner";
}
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
// inputs
const name = core.getInput('name', { required: true });
const file = core.getInput('file');
const skipNixBuild = core.getInput('skipNixBuild');
const attributes = core.getInput('attributes');
const nixBuildArgs = core.getInput('nixBuildArgs');
const name = core.getInput('name', { required: true });
const signingKey = core.getInput('signingKey');
const authToken = core.getInput('authToken');
const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix";
@ -51,43 +42,27 @@ function run() {
yield exec.exec('cachix', ['use', name]);
core.endGroup();
if (signingKey !== "") {
core.startGroup('Cachix: Configuring push');
// needed to discover auth token
yield exec.exec("sudo", ["sh", "-c", `echo export HOME=${home()} > /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `echo export CACHIX_SIGNING_KEY=${signingKey} >> /etc/nix/cachix-push.sh`]);
// needed to for nix-store
yield exec.exec("sudo", ["sh", "-c", `echo export PATH=\\$PATH:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/per-user/runner/profile/bin >> /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `echo ${cachixExecutable} push ${name} \\$OUT_PATHS >> /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `chmod +x /etc/nix/cachix-push.sh`]);
// enable post-build-hook
yield exec.exec("sudo", ["sh", "-c", `echo post-build-hook = /etc/nix/cachix-push.sh >> /etc/nix/nix.conf`]);
core.exportVariable('CACHIX_SIGNING_KEY', signingKey);
// Ignore reloading failures as Nix might be installed in single-user mode (install-nix-action version 5 or lower)
const options = { ignoreReturnCode: true };
// Reload nix-daemon
if (os_1.type() == "Darwin") {
// kickstart awaits nix-daemon to get up again
yield exec.exec("sudo", ["launchctl", "kickstart", "-k", "system/org.nixos.nix-daemon"], options);
if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`);
// Remember existing store paths
yield exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > store-path-pre-build`]);
let paths = '';
const options = {
listeners: {
stdout: (data) => {
paths += data.toString();
},
}
};
const args = strings_1.prependEach('-A', strings_1.nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]);
const additionalArgs = strings_1.nonEmptySplit(nixBuildArgs, /\s+/);
yield exec.exec('nix-build', additionalArgs.concat(args), options);
core.endGroup();
core.startGroup('Cachix: Pushing paths');
yield exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' | cat - store-path-pre-build | sort | uniq -u | ${cachixExecutable} push ${name}`]);
core.endGroup();
}
else {
yield exec.exec("sudo", ["pkill", "-HUP", "nix-daemon"], options);
}
core.endGroup();
}
if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`);
let paths = '';
const options = {
listeners: {
stdout: (data) => {
paths += data.toString();
},
}
};
const args = strings_1.prependEach('-A', strings_1.nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]);
const additionalArgs = strings_1.nonEmptySplit(nixBuildArgs, /\s+/);
yield exec.exec('nix-build', additionalArgs.concat(args), options);
core.endGroup();
}
}
catch (error) {

View file

@ -1,24 +1,17 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import { type } from 'os';
import { prependEach, nonEmptySplit } from './strings';
import { exit } from 'process';
function home() {
if (type() == "Darwin") {
return "/Users/runner";
} else {
return "/home/runner";
}
}
async function run() {
try {
// inputs
const name = core.getInput('name', { required: true });
const file = core.getInput('file');
const skipNixBuild = core.getInput('skipNixBuild');
const attributes = core.getInput('attributes');
const nixBuildArgs = core.getInput('nixBuildArgs');
const name = core.getInput('name', { required: true });
const signingKey = core.getInput('signingKey');
const authToken = core.getInput('authToken')
const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix";
@ -37,44 +30,31 @@ async function run() {
core.endGroup();
if (signingKey !== "") {
core.startGroup('Cachix: Configuring push');
// needed to discover auth token
await exec.exec("sudo", ["sh", "-c", `echo export HOME=${home()} > /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `echo export CACHIX_SIGNING_KEY=${signingKey} >> /etc/nix/cachix-push.sh`]);
// needed to for nix-store
await exec.exec("sudo", ["sh", "-c", `echo export PATH=\\$PATH:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/per-user/runner/profile/bin >> /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `echo ${cachixExecutable} push ${name} \\$OUT_PATHS >> /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `chmod +x /etc/nix/cachix-push.sh`]);
// enable post-build-hook
await exec.exec("sudo", ["sh", "-c", `echo post-build-hook = /etc/nix/cachix-push.sh >> /etc/nix/nix.conf`]);
core.exportVariable('CACHIX_SIGNING_KEY', signingKey);
// Ignore reloading failures as Nix might be installed in single-user mode (install-nix-action version 5 or lower)
const options = { ignoreReturnCode: true };
// Reload nix-daemon
if (type() == "Darwin") {
// kickstart awaits nix-daemon to get up again
await exec.exec("sudo", ["launchctl", "kickstart", "-k", "system/org.nixos.nix-daemon"], options);
} else {
await exec.exec("sudo", ["pkill", "-HUP", "nix-daemon"], options);
}
if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`);
core.endGroup();
}
if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`);
let paths = '';
const options = {
listeners: {
stdout: (data: Buffer) => {
paths += data.toString();
},
}
};
const args = prependEach('-A', nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]);
const additionalArgs = nonEmptySplit(nixBuildArgs, /\s+/);
await exec.exec('nix-build', additionalArgs.concat(args), options);
core.endGroup()
// Remember existing store paths
await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > store-path-pre-build`]);
let paths = '';
const options = {
listeners: {
stdout: (data: Buffer) => {
paths += data.toString();
},
}
};
const args = prependEach('-A', nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]);
const additionalArgs = nonEmptySplit(nixBuildArgs, /\s+/);
await exec.exec('nix-build', additionalArgs.concat(args), options);
core.endGroup()
core.startGroup('Cachix: Pushing paths');
await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' | cat - store-path-pre-build | sort | uniq -u | ${cachixExecutable} push ${name}`]);
core.endGroup();
}
}
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);