Support dry run in activation script

If the `DRY_RUN` variable is set then no actual change should be
performed. Only printing what actions would be taken.
This commit is contained in:
Robert Helgesson 2017-01-16 00:06:27 +01:00
parent a5c8362f7b
commit b1f84ada60
No known key found for this signature in database
GPG key ID: C3DB11069E65DC86
3 changed files with 29 additions and 9 deletions

View file

@ -168,7 +168,15 @@ in
internal = true; internal = true;
default = {}; default = {};
type = types.attrs; type = types.attrs;
description = "Activation scripts for the home environment."; description = ''
Activation scripts for the home environment.
</para><para>
Any script should respect the <varname>DRY_RUN</varname>
variable, if it is set then no actual action should be taken.
The variable <varname>DRY_RUN_CMD</varname> is set to
<code>echo</code> if dry run is enabled. Thus, many cases you
can use the idiom <code>$DRY_RUN_CMD rm -rf /</code>.
'';
}; };
home.activationPackage = mkOption { home.activationPackage = mkOption {
@ -202,8 +210,8 @@ in
for sourcePath in "$@" ; do for sourcePath in "$@" ; do
relativePath="$(realpath --relative-to "$newGenFiles" "$sourcePath")" relativePath="$(realpath --relative-to "$newGenFiles" "$sourcePath")"
targetPath="$HOME/$relativePath" targetPath="$HOME/$relativePath"
mkdir -vp "$(dirname "$targetPath")" $DRY_RUN_CMD mkdir -vp "$(dirname "$targetPath")"
ln -vsf "$sourcePath" "$targetPath" $DRY_RUN_CMD ln -vsf "$sourcePath" "$targetPath"
done done
''; '';
@ -219,8 +227,8 @@ in
echo " exists" echo " exists"
else else
echo " gone (deleting)" echo " gone (deleting)"
rm -v "$targetPath" $DRY_RUN_CMD rm -v "$targetPath"
rmdir --ignore-fail-on-non-empty -v -p "$(dirname "$targetPath")" $DRY_RUN_CMD rmdir --ignore-fail-on-non-empty -v -p "$(dirname "$targetPath")"
fi fi
done done
''; '';
@ -284,8 +292,8 @@ in
} }
if [[ "$oldGenPath" != "$newGenPath" ]] ; then if [[ "$oldGenPath" != "$newGenPath" ]] ; then
ln -Tsfv "$newGenPath" "$newGenProfilePath" $DRY_RUN_CMD ln -Tsfv "$newGenPath" "$newGenProfilePath"
ln -Tsfv "$newGenPath" "$newGenGcPath" $DRY_RUN_CMD ln -Tsfv "$newGenPath" "$newGenGcPath"
linkNewGen linkNewGen
cleanOldGen cleanOldGen
else else
@ -316,6 +324,14 @@ in
sf = pkgs.writeText "activation-script" '' sf = pkgs.writeText "activation-script" ''
#!${pkgs.stdenv.shell} #!${pkgs.stdenv.shell}
if [[ $DRY_RUN ]] ; then
echo "Performing dry run"
export DRY_RUN_CMD=echo
else
echo "Performing live run"
unset DRY_RUN_CMD
fi
${activationCmds} ${activationCmds}
''; '';

View file

@ -183,7 +183,11 @@ in
dconfPath = "/org/gnome/terminal/legacy/"; dconfPath = "/org/gnome/terminal/legacy/";
in in
'' ''
${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} < ${sf} if [[ $DRY_RUN ]]; then
echo ${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} "<" ${sf}
else
${pkgs.gnome3.dconf}/bin/dconf load ${dconfPath} < ${sf}
fi
''; '';
}; };
} }

View file

@ -120,7 +120,7 @@ in
fi fi
} }
systemctl --user daemon-reload $DRY_RUN_CMD systemctl --user daemon-reload
systemdPostReload systemdPostReload
''; '';
}; };