aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Scott <3648487+ayyjayess@users.noreply.github.com>2018-02-16 23:32:29 +0000
committerRobert Helgesson <robert@rycee.net>2018-07-27 22:07:12 +0200
commit30cba446f2c2e04db5a5001aaf606a4a5563e1de (patch)
tree71fd3e832b478ccb9cf81568b28a7b6c9c303c7f
parentdda65c0877b0b6c98d0f628374f2651d92597086 (diff)
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.
-rw-r--r--modules/files.nix18
-rw-r--r--modules/lib/file-type.nix11
-rw-r--r--modules/services/window-managers/i3.nix27
-rw-r--r--modules/services/window-managers/xmonad.nix25
4 files changed, 47 insertions, 34 deletions
diff --git a/modules/files.nix b/modules/files.nix
index c4978a1aeea..fd58027b36c 100644
--- a/modules/files.nix
+++ b/modules/files.nix
@@ -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 {
name = "home-manager-files";
diff --git a/modules/lib/file-type.nix b/modules/lib/file-type.nix
index 80d4ab45b2c..0c435aaa908 100644
--- a/modules/lib/file-type.nix
+++ b/modules/lib/file-type.nix
@@ -95,6 +95,17 @@ in
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 = {
diff --git a/modules/services/window-managers/i3.nix b/modules/services/window-managers/i3.nix
index 7b663a4fcae..84b5d25e65b 100644
--- a/modules/services/window-managers/i3.nix
+++ b/modules/services/window-managers/i3.nix
@@ -771,23 +771,16 @@ in
{
home.packages = [ cfg.package ];
xsession.windowManager.command = "${cfg.package}/bin/i3";
- xdg.configFile."i3/config".source = configFile;
-
- home.activation.checkI3 = dag.entryBefore [ "linkGeneration" ] ''
- if ! cmp --quiet \
- "${configFile}" \
- "${config.xdg.configHome}/i3/config"; then
- i3Changed=1
- 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
- '';
+ xdg.configFile."i3/config" = {
+ source = configFile;
+ onChange = ''
+ i3Socket=''${XDG_RUNTIME_DIR:-/run/user/$UID}/i3/ipc-socket.*
+ if [ -S $i3Socket ]; then
+ echo "Reloading i3"
+ $DRY_RUN_CMD ${cfg.package}/bin/i3-msg -s $i3Socket reload 1>/dev/null
+ fi
+ '';
+ };
}
(mkIf (cfg.config != null) {
diff --git a/modules/services/window-managers/xmonad.nix b/modules/services/window-managers/xmonad.nix
index d4643b09168..754e7f1fbf4 100644
--- a/modules/services/window-managers/xmonad.nix
+++ b/modules/services/window-managers/xmonad.nix
@@ -90,23 +90,14 @@ in
(mkIf (cfg.config != null) {
home.file.".xmonad/xmonad.hs".source = cfg.config;
-
- home.activation.checkXmonad = dag.entryBefore [ "linkGeneration" ] ''
- if ! cmp --quiet "${cfg.config}" "$HOME/.xmonad/xmonad.hs"; then
- xmonadChanged=1
- fi
- '';
-
- 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
+ home.file.".xmonad/xmonad.hs".onChange = ''
+ echo "Recompiling xmonad"
+ $DRY_RUN_CMD ${config.xsession.windowManager.command} --recompile
+
+ # Attempt to restart xmonad if X is running.
+ if [[ -v DISPLAY ]] ; then
+ echo "Restarting xmonad"
+ $DRY_RUN_CMD ${config.xsession.windowManager.command} --restart
fi
'';
})