diff options
Diffstat (limited to 'pkgs/populate/default.nix')
-rw-r--r-- | pkgs/populate/default.nix | 97 |
1 files changed, 81 insertions, 16 deletions
diff --git a/pkgs/populate/default.nix b/pkgs/populate/default.nix index 476e653..7129b90 100644 --- a/pkgs/populate/default.nix +++ b/pkgs/populate/default.nix @@ -1,7 +1,7 @@ with import ../../lib; with shell; -{ coreutils, dash, findutils, git, jq, openssh, pass, rsync, writers }: +{ coreutils, dash, findutils, git, jq, openssh, pass, passage, rsync, writers }: let check = { force, target }: let @@ -45,18 +45,34 @@ let ''; pop.file = target: source: let - configAttrs = ["useChecksum" "exclude" "filters" "deleteExcluded"]; - config = filterAttrs (name: _: elem name configAttrs) source; + config = rsyncDefaultConfig // derivedConfig // sourceConfig; + derivedConfig = { + useChecksum = + if isStorePath source.path + then true + else rsyncDefaultConfig.useChecksum; + }; + sourceConfig = + filterAttrs (name: _: elem name (attrNames rsyncDefaultConfig)) source; + sourcePath = + if isStorePath source.path + then quote (toString source.path) + else quote source.path; in - rsync' target config (quote source.path); + rsync' target config sourcePath; pop.git = target: source: runShell target /* sh */ '' set -efu + # Remove target path if it doesn't look like a git worktree. + # This can happen e.g. when it had a different type earlier. + if ! test -e ${quote target.path}/.git; then + rm -fR ${quote target.path} + fi if ! test -e ${quote target.path}; then ${if source.shallow then /* sh */ '' git init ${quote target.path} '' else /* sh */ '' - git clone --recurse-submodules ${quote source.url} ${quote target.path} + git clone --recurse-submodules ${quote source.url} ${quote target.path} ''} fi cd ${quote target.path} @@ -103,7 +119,15 @@ let umask 0077 if test -e ${quote source.dir}/.git; then - local_pass_info=${quote source.name}\ $(${git}/bin/git -C ${quote source.dir} log -1 --format=%H ${quote source.name}) + local_pass_info=${quote source.name}\ $( + ${git}/bin/git -C ${quote source.dir} log -1 --format=%H ${quote source.name} + # we append a hash for every symlink, otherwise we would miss updates on + # files where the symlink points to + ${findutils}/bin/find ${quote source.dir}/${quote source.name} -type l \ + -exec ${coreutils}/bin/realpath {} + | + ${coreutils}/bin/sort | + ${findutils}/bin/xargs -r -n 1 ${git}/bin/git -C ${quote source.dir} log -1 --format=%H + ) remote_pass_info=$(${runShell target /* sh */ '' cat ${quote target.path}/.pass_info || : ''}) @@ -119,28 +143,63 @@ let rm -fR "$tmp_dir" } - ${findutils}/bin/find ${quote passPrefix} -type f -follow | + ${findutils}/bin/find ${quote passPrefix} -type f -follow ! -name .gpg-id | while read -r gpg_path; do rel_name=''${gpg_path#${quote passPrefix}} rel_name=''${rel_name%.gpg} pass_date=$( - ${git}/bin/git -C ${quote source.dir} log -1 --format=%aI "$gpg_path" + if test -e ${quote source.dir}/.git; then + ${git}/bin/git -C ${quote source.dir} log -1 --format=%aI "$gpg_path" + fi ) pass_name=${quote source.name}/$rel_name tmp_path=$tmp_dir/$rel_name ${coreutils}/bin/mkdir -p "$(${coreutils}/bin/dirname "$tmp_path")" PASSWORD_STORE_DIR=${quote source.dir} ${pass}/bin/pass show "$pass_name" > "$tmp_path" - ${coreutils}/bin/touch -d "$pass_date" "$tmp_path" + if [ -n "$pass_date" ]; then + ${coreutils}/bin/touch -d "$pass_date" "$tmp_path" + fi done if test -n "''${local_pass_info-}"; then echo "$local_pass_info" > "$tmp_dir"/.pass_info fi - ${rsync' target {} /* sh */ "$tmp_dir"} + ${rsync' target rsyncDefaultConfig /* sh */ "$tmp_dir"} + ''; + + pop.passage = target: source: /* sh */ '' + set -efu + + export PASSAGE_AGE=${quote source.age} + export PASSAGE_DIR=${quote source.dir} + export PASSAGE_IDENTITIES_FILE=${quote source.identities_file} + + umask 0077 + + tmp_dir=$(${coreutils}/bin/mktemp -dt populate-passage.XXXXXXXX) + trap cleanup EXIT + cleanup() { + rm -fR "$tmp_dir" + } + + ${findutils}/bin/find "$PASSAGE_DIR" -type f -name \*.age -follow | + while read -r age_path; do + + rel_name=''${age_path#$PASSAGE_DIR} + rel_name=''${rel_name%.age} + + tmp_path=$tmp_dir/$rel_name + + ${coreutils}/bin/mkdir -p "$(${coreutils}/bin/dirname "$tmp_path")" + ${passage}/bin/passage show "$rel_name" > "$tmp_path" + ${coreutils}/bin/touch -r "$age_path" "$tmp_path" + done + + ${rsync' target rsyncDefaultConfig /* sh */ "$tmp_dir"} ''; pop.pipe = target: source: /* sh */ '' @@ -168,17 +227,17 @@ let source_path=$source_path/ fi ${rsync}/bin/rsync \ - ${optionalString (config.useChecksum or false) /* sh */ "--checksum"} \ + ${optionalString config.useChecksum /* sh */ "--checksum"} \ ${optionalString target.sudo /* sh */ "--rsync-path=\"sudo rsync\""} \ ${concatMapStringsSep " " (pattern: /* sh */ "--exclude ${quote pattern}") - (config.exclude or [])} \ + config.exclude} \ ${concatMapStringsSep " " (filter: /* sh */ "--${filter.type} ${quote filter.pattern}") - (config.filters or [])} \ + config.filters} \ -e ${quote (ssh' target)} \ -vFrlptD \ - ${optionalString (config.deleteExcluded or true) /* sh */ "--delete-excluded"} \ + ${optionalString config.deleteExcluded /* sh */ "--delete-excluded"} \ "$source_path" \ ${quote ( optionalString (!isLocalTarget target) ( @@ -190,6 +249,13 @@ let >&2 ''; + rsyncDefaultConfig = { + useChecksum = false; + exclude = []; + filters = []; + deleteExcluded = true; + }; + runShell = target: command: if isLocalTarget target then command @@ -202,8 +268,7 @@ let ssh' = target: concatMapStringsSep " " quote (flatten [ "${openssh}/bin/ssh" - (optionals (target.user != "") ["-l" target.user]) - "-p" target.port + (mkUserPortSSHOpts target) "-T" target.extraOptions ]); |