From dfe66f0170e4dfee4742909740d0cdcd28ed7544 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 24 Nov 2026 01:34:04 +0000 Subject: krops.nix: link or shallow-clone git sources for evaluation --- krops.nix | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'krops.nix') diff --git a/krops.nix b/krops.nix index 92445372..0ab65cdd 100644 --- a/krops.nix +++ b/krops.nix @@ -19,7 +19,8 @@ # A host whose spec.nix lists builders (ssh destinations, e.g. # [ "root@zu.r" ]) gets evaluated on the deploying host, from sources # populated into $HOST_CACHEDIR/src/HOSTNAME (default: ~/tmp/cache/hrm; -# nixpkgs as a symlink to when that's at the pinned revision), +# git sources as symlinks to this host's checkouts of the same name at +# the same revision, else shallow clones), # and built on the first builder that answers. The builder keeps a GC # root in /var/hrm/HOSTNAME/system and copies the system to the target # (or, with copyVia = "deployer", the deploying host copies it from the @@ -163,7 +164,6 @@ rec { q = lib.escapeShellArg; nix = "nix --extra-experimental-features nix-command"; jq = "${pkgs.jq}/bin/jq"; - git = "${pkgs.git}/bin/git"; targetDest = lib.optionalString (target'.user != null) "${target'.user}@" + target'.host; targetSSHOpts = @@ -171,15 +171,38 @@ rec { targetURL = "${spec.targetStore}://${targetDest}"; targetSource = lib.evalSource [ targetSources ]; # Sources for evaluating here, in a directory of this host, populated as - # onto a target: everything but targetSources and nixpkgs. + # 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. hostCache = let dir = builtins.getEnv "HOST_CACHEDIR"; in if dir != "" then dir else toString ~/tmp/cache/hrm; evalTarget = lib.mkTarget "${hostCache}/src/${name}"; + # The revision of a checkout: its Git HEAD, or nixpkgs' .git-revision. + revisionOf = path: let + git = "${path}/.git"; + revisionFile = "${path}/.git-revision"; + in + if lib.pathIsGitRepo git then lib.commitIdFromGitRepo git + else if builtins.pathExists revisionFile then lib.fileContents revisionFile + else null; + # This host's , if it's at revision REV. + hereAt = name: rev: let + found = builtins.tryEval (builtins.findFile builtins.nixPath name); + in + if found.success && revisionOf (toString found.value) == rev + then toString found.value + else null; evalSource = lib.evalSource [ - (removeAttrs sources (lib.attrNames targetSources ++ [ "nixpkgs" ])) + (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; }; } + else source + ) (removeAttrs sources (lib.attrNames targetSources))) ]; - nixpkgsSource = lib.evalSource [ { inherit (sources) nixpkgs; } ]; # Runs SCRIPT on the target, with the system's store path on standard # input (read -r system). onTarget = script: /* sh */ '' @@ -201,33 +224,11 @@ rec { # # Sources for evaluating here, but nothing of targetSources: evaluation # must not resolve here, where it would yield this host's - # paths. nixpkgs: this host's if it's at the pinned revision, - # as a symlink, else populated like any other git source. + # paths. # eval_dir=${q evalTarget.path} mkdir -p "$eval_dir" ${pkgs.populate { source = evalSource; target = evalTarget; }} - nixpkgs=$(nix-instantiate --find-file nixpkgs 2>/dev/null) || nixpkgs= - nixpkgs_has= - if test -z "$nixpkgs"; then - : - elif test -e "$nixpkgs"/.git-revision; then - nixpkgs_has=$(cat "$nixpkgs"/.git-revision) - elif ${git} -C "$nixpkgs" rev-parse --git-dir >/dev/null 2>&1 && - test -z "$(${git} -C "$nixpkgs" status --porcelain --untracked-files=no)"; then - nixpkgs_has=$(${git} -C "$nixpkgs" rev-parse HEAD) - fi - if test "$nixpkgs_has" = ${q sources.nixpkgs.git.ref} && - { test -L "$eval_dir"/nixpkgs || ! test -e "$eval_dir"/nixpkgs; } - then - ln -sfn "$nixpkgs" "$eval_dir"/nixpkgs - else - # populate mustn't follow the symlink into this host's own checkout. - if test -L "$eval_dir"/nixpkgs; then - rm "$eval_dir"/nixpkgs - fi - ${pkgs.populate { source = nixpkgsSource; target = evalTarget; }} - fi export NIX_PATH="$eval_dir" # -- cgit v1.3.1