From eaf867558df8e417bf2e028a7f55bb6e9d61439c Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 27 Nov 2026 20:51:14 +0000 Subject: krops.nix: evaluate as on the target A host with builders gets evaluated here, but with its sources mounted where the target has them, so that paths like are the target's, as if it had evaluated itself, and nothing needs to be told where the secrets are. This host's checkouts get mounted there as well, instead of linked: a link would lead to this host's path, and one into the target's path, where this host has its own sources, to itself. --- krops.nix | 75 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 25 deletions(-) (limited to 'krops.nix') diff --git a/krops.nix b/krops.nix index 412e8d81..b93b8673 100644 --- a/krops.nix +++ b/krops.nix @@ -18,10 +18,11 @@ # REMOTE BUILDS # A host whose spec.nix lists builders (targets like populate's, # [USER@]HOST[:PORT]/PATH, e.g. [ "tv@zu.r/home/tv/tmp/cache/hrm/roots" ]) -# gets evaluated on the deploying host, from sources populated into -# $HOST_CACHEDIR/src/HOSTNAME (default: ~/tmp/cache/hrm; -# git sources as symlinks to this host's checkouts of the same name at -# the same revision, else shallow clones), +# gets evaluated on the deploying host, as on the target: from sources +# populated into $HOST_CACHEDIR/src/HOSTNAME (default: ~/tmp/cache/hrm; +# git sources as shallow clones, but for those this host has a checkout +# of, of the same name at the same revision), which the evaluation sees +# at the target's path, with those checkouts (mounted, by bubblewrap), # and built on the first builder that answers. While deploying, the # builder keeps a GC root in PATH/HOSTNAME (as the user it's logged in # as, not necessarily root) and copies the system to the target @@ -207,9 +208,7 @@ rec { }) spec.builders; targetSource = lib.evalSource [ targetSources ]; # Sources for evaluating here, in a directory of this host, populated as - # onto a target: everything but targetSources. Git sources link to this - # host's checkout of the same name at the same revision, if it has one, - # else get cloned, shallow until populate shares Git objects. + # onto a target: everything but targetSources and mountSources. hostCache = let dir = builtins.getEnv "HOST_CACHEDIR"; in if dir != "" then dir else toString ~/tmp/cache/hrm; @@ -229,15 +228,24 @@ rec { if found.success && revisionOf (toString found.value) == rev then toString found.value else null; + # The Git sources this host has a checkout of at the same revision, by + # name: the evaluation gets them mounted. + mountSources = lib.filterAttrs (_: path: path != null) + (lib.mapAttrs + (name: source: + if source.git or null != null + then hereAt name source.git.ref + else null) + (removeAttrs sources (lib.attrNames targetSources))); + # The rest gets populated, Git sources as shallow clones until populate + # shares Git objects. evalSource = lib.evalSource [ (lib.mapAttrs (name: source: - if source ? git then - let here = hereAt name source.git.ref; in - if here != null - then { symlink.target = here; } - else { git = source.git // { shallow = true; }; } + if source.git or null != null + then { git = source.git // { shallow = true; }; } else source - ) (removeAttrs sources (lib.attrNames targetSources))) + ) (removeAttrs sources + (lib.attrNames targetSources ++ lib.attrNames mountSources))) ]; # Runs SCRIPT on the target, with the system's store path on standard # input (read -r system). @@ -258,13 +266,19 @@ rec { mkdir -p "$portable_cache" "$host_cache"/roots # - # Sources for evaluating here, but nothing of targetSources: evaluation - # must not resolve here, where it would yield this host's - # paths. + # Sources for evaluating here. targetSources and mountSources get + # empty directories: the former so that evaluation finds where they + # are, but nothing in them, the latter as mount points. # eval_dir=${q evalTarget.path} ${pkgs.populate { source = evalSource; target = evalTarget; force = true; }} - export NIX_PATH="$eval_dir" + for source in ${lib.escapeShellArgs + (lib.attrNames targetSources ++ lib.attrNames mountSources)}; do + if test -L "$eval_dir/$source"; then + rm "$eval_dir/$source" + fi + mkdir -p "$eval_dir/$source" + done # # Choose the first builder that answers, and note its state. @@ -298,16 +312,27 @@ rec { builder_system=$5 # - # Evaluate here, keeping the derivation for diff. + # Evaluate here, with the sources mounted where the target has them, so + # that it evaluates as on the target. Keep the derivation for diff. # + if ! test -d ${q target'.path}; then + echo "$name: error: no "${q target'.path}" here to mount the sources at" >&2 + exit 1 + fi echo "$name: evaluating..." >&2 - drv=$(nix-instantiate \ - --show-trace \ - --add-root "$host_cache"/roots/"$name".drv \ - --argstr buildSystem "$builder_system" \ - --argstr secretDirectory ${q "${target'.path}/secrets"} \ - --attr nixosConfigurations."$name".config.system.build.toplevel \ - '') + drv=$(${pkgs.bubblewrap}/bin/bwrap \ + --dev-bind / / \ + --bind "$eval_dir" ${q target'.path} \ + ${lib.concatStrings (lib.mapAttrsToList (name: path: + "--ro-bind ${q path} ${q "${target'.path}/${name}"} " + ) mountSources)}\ + --setenv NIX_PATH ${q target'.path} \ + nix-instantiate \ + --show-trace \ + --add-root "$host_cache"/roots/"$name".drv \ + --argstr buildSystem "$builder_system" \ + --attr nixosConfigurations."$name".config.system.build.toplevel \ + '') # nix-instantiate prints the GC root, a link to the derivation here. drv=$(readlink -f -- "$drv") -- cgit v1.3.1