files: add onChange option

This option allows execution of arbitrary shell code when a file that
is linked into the home directory has been changed between
generations.
This commit is contained in:
Andrew Scott 2018-02-16 23:32:29 +00:00 committed by Robert Helgesson
parent dda65c0877
commit 30cba446f2
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
4 changed files with 46 additions and 33 deletions

View file

@ -197,6 +197,24 @@ in
'' ''
); );
home.activation.checkFilesChanged = dag.entryBefore ["linkGeneration"] (
''
declare -A changedFiles
'' + concatMapStrings (v: ''
cmp --quiet "${v.source}" "${config.home.homeDirectory}/${v.target}" \
&& changedFiles["${v.target}"]=0 \
|| changedFiles["${v.target}"]=1
'') (filter (v: v.onChange != "") (attrValues cfg))
);
home.activation.onFilesChange = dag.entryAfter ["linkGeneration"] (
concatMapStrings (v: ''
if [[ ${"$\{changedFiles"}["${v.target}"]} -eq 1 ]]; then
${v.onChange}
fi
'') (filter (v: v.onChange != "") (attrValues cfg))
);
home-files = pkgs.stdenv.mkDerivation { home-files = pkgs.stdenv.mkDerivation {
name = "home-manager-files"; name = "home-manager-files";

View file

@ -95,6 +95,17 @@ in
are symbolic links to the files of the source directory. are symbolic links to the files of the source directory.
''; '';
}; };
onChange = mkOption {
type = types.lines;
default = "";
description = ''
Shell commands to run when file has changed between
generations. The script will be run
<emphasis>after</emphasis> the new files have been linked
into place.
'';
};
}; };
config = { config = {

View file

@ -771,23 +771,16 @@ in
{ {
home.packages = [ cfg.package ]; home.packages = [ cfg.package ];
xsession.windowManager.command = "${cfg.package}/bin/i3"; xsession.windowManager.command = "${cfg.package}/bin/i3";
xdg.configFile."i3/config".source = configFile; xdg.configFile."i3/config" = {
source = configFile;
home.activation.checkI3 = dag.entryBefore [ "linkGeneration" ] '' onChange = ''
if ! cmp --quiet \ i3Socket=''${XDG_RUNTIME_DIR:-/run/user/$UID}/i3/ipc-socket.*
"${configFile}" \ if [ -S $i3Socket ]; then
"${config.xdg.configHome}/i3/config"; then echo "Reloading i3"
i3Changed=1 $DRY_RUN_CMD ${cfg.package}/bin/i3-msg -s $i3Socket reload 1>/dev/null
fi fi
''; '';
};
home.activation.reloadI3 = dag.entryAfter [ "linkGeneration" ] ''
SOCKET=''${XDG_RUNTIME_DIR:-/run/user/$UID}/i3/ipc-socket.*
if [ -v i3Changed ] && [ -S $SOCKET ]; then
echo "Reloading i3"
${cfg.package}/bin/i3-msg -s $SOCKET reload 1>/dev/null
fi
'';
} }
(mkIf (cfg.config != null) { (mkIf (cfg.config != null) {

View file

@ -90,23 +90,14 @@ in
(mkIf (cfg.config != null) { (mkIf (cfg.config != null) {
home.file.".xmonad/xmonad.hs".source = cfg.config; home.file.".xmonad/xmonad.hs".source = cfg.config;
home.file.".xmonad/xmonad.hs".onChange = ''
echo "Recompiling xmonad"
$DRY_RUN_CMD ${config.xsession.windowManager.command} --recompile
home.activation.checkXmonad = dag.entryBefore [ "linkGeneration" ] '' # Attempt to restart xmonad if X is running.
if ! cmp --quiet "${cfg.config}" "$HOME/.xmonad/xmonad.hs"; then if [[ -v DISPLAY ]] ; then
xmonadChanged=1 echo "Restarting xmonad"
fi $DRY_RUN_CMD ${config.xsession.windowManager.command} --restart
'';
home.activation.applyXmonad = dag.entryAfter [ "linkGeneration" ] ''
if [[ -v xmonadChanged ]]; then
echo "Recompiling xmonad"
${config.xsession.windowManager.command} --recompile
# Attempt to restart xmonad if X is running.
if [[ -v DISPLAY ]] ; then
echo "Restarting xmonad"
${config.xsession.windowManager.command} --restart
fi
fi fi
''; '';
}) })