1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# 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;
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
'';
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
|