From 30cba446f2c2e04db5a5001aaf606a4a5563e1de Mon Sep 17 00:00:00 2001 From: Andrew Scott <3648487+ayyjayess@users.noreply.github.com> Date: Fri, 16 Feb 2018 23:32:29 +0000 Subject: [PATCH] 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. --- modules/files.nix | 18 ++++++++++++++ modules/lib/file-type.nix | 11 +++++++++ modules/services/window-managers/i3.nix | 27 ++++++++------------- modules/services/window-managers/xmonad.nix | 23 ++++++------------ 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/modules/files.nix b/modules/files.nix index c4978a1ae..fd58027b3 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 80d4ab45b..0c435aaa9 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 + after 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 7b663a4fc..84b5d25e6 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 d4643b091..754e7f1fb 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.file.".xmonad/xmonad.hs".onChange = '' + echo "Recompiling xmonad" + $DRY_RUN_CMD ${config.xsession.windowManager.command} --recompile - 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 + # Attempt to restart xmonad if X is running. + if [[ -v DISPLAY ]] ; then + echo "Restarting xmonad" + $DRY_RUN_CMD ${config.xsession.windowManager.command} --restart fi ''; })