diff options
| author | tv <tv@krebsco.de> | 2026-11-14 00:27:45 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-14 01:46:01 +0100 |
| commit | a4540a584d762b000b21cb7e61987c414eb72a1d (patch) | |
| tree | 077bb7dfc4d104e5e133ad96c4485ff99bcd14c5 /pkgs/populate/default.nix | |
| parent | 8f08426ae3a2e5f0ecc5d010bc6469b41e3d7678 (diff) | |
populate: layer populate below krops
populate is a program of its own, with a Nix library, in pkgs/populate,
which krops uses, and which doesn't use krops:
- default.nix builds the program, with nix-build there or callPackage,
as pkgs.populate-unwrapped.
- lib is populate's Nix library: the types of sources and the POSIX
types they use, evalSource, flakeFollows, hasGeneratedFlake, and
derivesFlake, taking nixpkgs' lib. krops' lib takes them from there,
so it provides them as before, and so does lib/types.
pkgs.populate, the function from populate's arguments to a script
running the program, is krops' own, in pkgs/krops/populate.nix, with
checks.nix next to it. It takes the program as populate-unwrapped,
which callPackage passes from the overlay; the flake, whose nixpkgs
lacks it, passes its own.
Diffstat (limited to 'pkgs/populate/default.nix')
| -rw-r--r-- | pkgs/populate/default.nix | 106 |
1 files changed, 24 insertions, 82 deletions
diff --git a/pkgs/populate/default.nix b/pkgs/populate/default.nix index bd8e772..0c98b28 100644 --- a/pkgs/populate/default.nix +++ b/pkgs/populate/default.nix @@ -1,86 +1,28 @@ -# Populate, which delivers sources to a target directory. Given populate's -# arguments, it checks them, and returns a script running the program in -# this directory with them, the sources as JSON. rustProgram is that program, e.g. one -# built with cargo during development. -{ coreutils, findutils, git, nix, openssh, pass, passage, rsync, writers -, rustPlatform ? null -, rustProgram ? import ./program.nix { inherit rustPlatform; } -, ... -}@args: - -with import ../../lib; +# populate, built from the crate in this directory: nix-build here, or +# callPackage. +{ rustPlatform ? (import <nixpkgs> {}).rustPlatform }: let - checks = import ./checks.nix; - - # Arguments populate doesn't use, which overrides written for krops 1 may - # set. They come through ..., because callPackage would set named ones. - unused = filter (name: args ? ${name}) [ "dash" "implementation" "jq" ]; - unexpected = attrNames (removeAttrs args ([ - "coreutils" "findutils" "git" "nix" "openssh" "pass" "passage" "rsync" - "writers" "rustPlatform" "rustProgram" - ] ++ unused)); - - # The parts of a target string, like mkTarget parses it, without its - # defaults, which populate fills in when it runs. - parseTarget = s: let - parts = match "(([^@]*)@)?(([^:/]+))?(:([^/]+))?(/.*)?" s; - in - if parts == null then throw "krops: invalid target ${toJSON s}" - else { host = elemAt parts 3; path = elemAt parts 6; }; - - populate = - { backup ? false, force ? false, prune ? false, source, target }: - let - # A target string goes to populate as it is, so populate decides what - # it leaves out when it runs. An attribute set, as mkTarget returns - # it, goes part by part, with whether it's local decided now, like the - # other functions of krops do. - parsed = parseTarget target; - path = if isString target then (if parsed.path != null then parsed.path else "/var/src") else target.path; - host = if isString target then (if parsed.host != null then parsed.host else "localhost") else target.host; - targetArgs = - if isString target then [ target ] - else [ - "--user" (if target.user or null == null then "" else target.user) - "--host" target.host - "--path" target.path - ] ++ optionals (target.port or null != null) [ - "--port" (toString target.port) - ] ++ optional (target.sudo or false) "--sudo" - ++ concatMap (arg: [ "--ssh-arg" arg ]) (flatten (target.extraOptions or [])) - ++ [ (if isLocalTarget target then "--local" else "--remote") ]; - in - assert checks.checkTargetPath path; - assert all checks.checkSourceName (attrNames source); - assert checks.checkInputs source; - assert isString target || checks.checkCopyingWithSudo target source; - writers.writeDash "populate.${host}" /* sh */ '' - set -efu - echo + "$0" >&2 - PATH=${makeBinPath [ - coreutils findutils git nix openssh pass passage rsync - ]}''${PATH:+:$PATH} - export PATH - # The sources on a file descriptor of their own, so the commands - # populate runs keep standard input. Store paths in them stay - # dependencies of this script, so building it builds them. The JSON is - # a single line, which can't be the delimiter. - exec ${rustProgram}/bin/populate ${escapeShellArgs ( - optional backup "--backup" ++ - optional force "--force" ++ - optional prune "--prune" - )} /dev/fd/3 ${escapeShellArgs targetArgs} 3<<'EOF' - ${toJSON source} - EOF - ''; - + cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); in -if unexpected != [] then - throw "krops: populate called with unexpected argument ${head unexpected}" -else - foldr - (name: warn "krops: populate's argument ${name} is unused") - populate - unused +rustPlatform.buildRustPackage { + pname = cargoToml.package.name; + inherit (cargoToml.package) version; + # Without what cargo doesn't build from, so changes to the Nix files, the + # docs, or the tests don't rebuild the program. By names, not by paths + # relative to this directory: the paths the filter gets needn't start + # with toString ./., e.g. if a symlink leads to this directory. + src = builtins.path { + name = "populate-src"; + path = ./.; + filter = path: type: let + name = baseNameOf path; + in + !(builtins.elem name [ + ".gitignore" "README.md" "docs" "lib" "target" "tests" + ] || + builtins.match ".*\\.nix" name != null); + }; + cargoLock.lockFile = ./Cargo.lock; +} |
