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 install --frozen-lockfile
- run: yarn build - run: yarn build
- run: yarn test - run: yarn test
- uses: cachix/install-nix-action@v6 - uses: cachix/install-nix-action@v7
- name: Test public cache - name: Test public cache
uses: ./ uses: ./
with: with:

View file

@ -18,25 +18,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const os_1 = require("os");
const strings_1 = require("./strings"); const strings_1 = require("./strings");
function home() {
if (os_1.type() == "Darwin") {
return "/Users/runner";
}
else {
return "/home/runner";
}
}
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
// inputs // inputs
const name = core.getInput('name', { required: true });
const file = core.getInput('file'); const file = core.getInput('file');
const skipNixBuild = core.getInput('skipNixBuild'); const skipNixBuild = core.getInput('skipNixBuild');
const attributes = core.getInput('attributes'); const attributes = core.getInput('attributes');
const nixBuildArgs = core.getInput('nixBuildArgs'); const nixBuildArgs = core.getInput('nixBuildArgs');
const name = core.getInput('name', { required: true });
const signingKey = core.getInput('signingKey'); const signingKey = core.getInput('signingKey');
const authToken = core.getInput('authToken'); const authToken = core.getInput('authToken');
const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix"; const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix";
@ -51,31 +42,11 @@ function run() {
yield exec.exec('cachix', ['use', name]); yield exec.exec('cachix', ['use', name]);
core.endGroup(); core.endGroup();
if (signingKey !== "") { 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); 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);
}
else {
yield exec.exec("sudo", ["pkill", "-HUP", "nix-daemon"], options);
}
core.endGroup();
}
if (skipNixBuild !== 'true') { if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`); 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 = ''; let paths = '';
const options = { const options = {
listeners: { listeners: {
@ -88,6 +59,10 @@ function run() {
const additionalArgs = strings_1.nonEmptySplit(nixBuildArgs, /\s+/); const additionalArgs = strings_1.nonEmptySplit(nixBuildArgs, /\s+/);
yield exec.exec('nix-build', additionalArgs.concat(args), options); yield exec.exec('nix-build', additionalArgs.concat(args), options);
core.endGroup(); 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();
}
} }
} }
catch (error) { catch (error) {

View file

@ -1,24 +1,17 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import { type } from 'os';
import { prependEach, nonEmptySplit } from './strings'; import { prependEach, nonEmptySplit } from './strings';
import { exit } from 'process';
function home() {
if (type() == "Darwin") {
return "/Users/runner";
} else {
return "/home/runner";
}
}
async function run() { async function run() {
try { try {
// inputs // inputs
const name = core.getInput('name', { required: true });
const file = core.getInput('file'); const file = core.getInput('file');
const skipNixBuild = core.getInput('skipNixBuild'); const skipNixBuild = core.getInput('skipNixBuild');
const attributes = core.getInput('attributes'); const attributes = core.getInput('attributes');
const nixBuildArgs = core.getInput('nixBuildArgs'); const nixBuildArgs = core.getInput('nixBuildArgs');
const name = core.getInput('name', { required: true });
const signingKey = core.getInput('signingKey'); const signingKey = core.getInput('signingKey');
const authToken = core.getInput('authToken') const authToken = core.getInput('authToken')
const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix"; const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix";
@ -37,32 +30,14 @@ async function run() {
core.endGroup(); core.endGroup();
if (signingKey !== "") { 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); 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);
}
core.endGroup();
}
if (skipNixBuild !== 'true') { if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`); core.startGroup(`Invoking nix-build`);
// Remember existing store paths
await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > store-path-pre-build`]);
let paths = ''; let paths = '';
const options = { const options = {
listeners: { listeners: {
@ -75,6 +50,11 @@ async function run() {
const additionalArgs = nonEmptySplit(nixBuildArgs, /\s+/); const additionalArgs = nonEmptySplit(nixBuildArgs, /\s+/);
await exec.exec('nix-build', additionalArgs.concat(args), options); await exec.exec('nix-build', additionalArgs.concat(args), options);
core.endGroup() 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) { } catch (error) {
core.setFailed(`Action failed with error: ${error}`); core.setFailed(`Action failed with error: ${error}`);