From 092b81b95615919b36cbb0e690dda8583b31013e Mon Sep 17 00:00:00 2001 From: Varun Narravula Date: Tue, 8 Oct 2024 12:39:25 -0700 Subject: [PATCH 1/5] atuin: configure daemon using systemd and launchd This configures the atuin daemon for Linux and Darwin systems using systemd and launchd, respectively. For systemd, a socket is also automatically configured to exist at atuin's default socket location. --- modules/programs/atuin.nix | 137 +++++++++++++++++++++++++++---------- 1 file changed, 99 insertions(+), 38 deletions(-) diff --git a/modules/programs/atuin.nix b/modules/programs/atuin.nix index a27bcf860..e88dd94f3 100644 --- a/modules/programs/atuin.nix +++ b/modules/programs/atuin.nix @@ -8,6 +8,7 @@ let tomlFormat = pkgs.formats.toml { }; + inherit (pkgs.stdenv) isLinux isDarwin; in { meta.maintainers = [ maintainers.hawkw ]; @@ -94,49 +95,109 @@ in { Whether to enable Nushell integration. ''; }; + + daemon.enable = mkEnableOption "atuin daemon"; }; config = let flagsStr = escapeShellArgs cfg.flags; - in mkIf cfg.enable { + in mkIf cfg.enable (mkMerge [ + { + # Always add the configured `atuin` package. + home.packages = [ cfg.package ]; - # Always add the configured `atuin` package. - home.packages = [ cfg.package ]; + # If there are user-provided settings, generate the config file. + xdg.configFile."atuin/config.toml" = mkIf (cfg.settings != { }) { + source = tomlFormat.generate "atuin-config" cfg.settings; + }; - # If there are user-provided settings, generate the config file. - xdg.configFile."atuin/config.toml" = mkIf (cfg.settings != { }) { - source = tomlFormat.generate "atuin-config" cfg.settings; - }; - - programs.bash.initExtra = mkIf cfg.enableBashIntegration '' - if [[ :$SHELLOPTS: =~ :(vi|emacs): ]]; then - source "${pkgs.bash-preexec}/share/bash/bash-preexec.sh" - eval "$(${lib.getExe cfg.package} init bash ${flagsStr})" - fi - ''; - - programs.zsh.initExtra = mkIf cfg.enableZshIntegration '' - if [[ $options[zle] = on ]]; then - eval "$(${lib.getExe cfg.package} init zsh ${flagsStr})" - fi - ''; - - programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration '' - ${lib.getExe cfg.package} init fish ${flagsStr} | source - ''; - - programs.nushell = mkIf cfg.enableNushellIntegration { - extraEnv = '' - let atuin_cache = "${config.xdg.cacheHome}/atuin" - if not ($atuin_cache | path exists) { - mkdir $atuin_cache - } - ${ - lib.getExe cfg.package - } init nu ${flagsStr} | save --force ${config.xdg.cacheHome}/atuin/init.nu + programs.bash.initExtra = mkIf cfg.enableBashIntegration '' + if [[ :$SHELLOPTS: =~ :(vi|emacs): ]]; then + source "${pkgs.bash-preexec}/share/bash/bash-preexec.sh" + eval "$(${lib.getExe cfg.package} init bash ${flagsStr})" + fi ''; - extraConfig = '' - source ${config.xdg.cacheHome}/atuin/init.nu + + programs.zsh.initExtra = mkIf cfg.enableZshIntegration '' + if [[ $options[zle] = on ]]; then + eval "$(${lib.getExe cfg.package} init zsh ${flagsStr})" + fi ''; - }; - }; + + programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration '' + ${lib.getExe cfg.package} init fish ${flagsStr} | source + ''; + + programs.nushell = mkIf cfg.enableNushellIntegration { + extraEnv = '' + let atuin_cache = "${config.xdg.cacheHome}/atuin" + if not ($atuin_cache | path exists) { + mkdir $atuin_cache + } + ${ + lib.getExe cfg.package + } init nu ${flagsStr} | save --force ${config.xdg.cacheHome}/atuin/init.nu + ''; + extraConfig = '' + source ${config.xdg.cacheHome}/atuin/init.nu + ''; + }; + } + + (mkIf cfg.daemon.enable (mkMerge [ + { + assertions = [{ + assertion = isLinux || isDarwin; + message = + "The atuin daemon can only be configured on either Linux or macOS."; + }]; + + programs.atuin.settings = { daemon = { enabled = true; }; }; + } + (mkIf isLinux { + programs.atuin.settings = { daemon = { systemd_socket = true; }; }; + + systemd.user.services.atuin-daemon = { + Unit = { + Description = "atuin daemon"; + Requires = [ "atuin-daemon.socket" ]; + }; + Install = { + Also = [ "atuin-daemon.socket" ]; + WantedBy = [ "default.target" ]; + }; + Service = { + ExecStart = "${lib.getExe cfg.package} daemon"; + Environment = [ "ATUIN_LOG=info" ]; + Restart = "on-failure"; + RestartSteps = 3; + RestartMaxDelaySec = 6; + }; + }; + + systemd.user.sockets.atuin-daemon = { + Unit = { Description = "atuin daemon socket"; }; + Install = { WantedBy = [ "sockets.target" ]; }; + Socket = { + ListenStream = "%h/.local/share/atuin/atuin.sock"; + SocketMode = "0600"; + RemoveOnStop = true; + }; + }; + }) + (mkIf isDarwin { + launchd.agents.atuin-daemon = { + enable = true; + config = { + ProgramArguments = [ "${lib.getExe cfg.package}" "daemon" ]; + EnvironmentVariables = { ATUIN_LOG = "info"; }; + KeepAlive = { + Crashed = true; + SuccessfulExit = false; + }; + ProcessType = "Background"; + }; + }; + }) + ])) + ]); } From 33c236f1d5eb3d1a3df202540794d590c2fe0a2f Mon Sep 17 00:00:00 2001 From: Varun Narravula Date: Tue, 8 Oct 2024 14:34:09 -0700 Subject: [PATCH 2/5] atuin: add water-sucks as maintainer --- modules/programs/atuin.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/programs/atuin.nix b/modules/programs/atuin.nix index e88dd94f3..b7e7f8808 100644 --- a/modules/programs/atuin.nix +++ b/modules/programs/atuin.nix @@ -10,7 +10,7 @@ let inherit (pkgs.stdenv) isLinux isDarwin; in { - meta.maintainers = [ maintainers.hawkw ]; + meta.maintainers = [ maintainers.hawkw maintainers.water-sucks ]; options.programs.atuin = { enable = mkEnableOption "atuin"; From c56aa0f51d058f41a7ba0c45bd3b6d9d244c0396 Mon Sep 17 00:00:00 2001 From: Varun Narravula Date: Sat, 19 Oct 2024 16:59:15 -0700 Subject: [PATCH 3/5] atuin: assert version >= 18.2.0 when daemon is enabled --- modules/programs/atuin.nix | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/programs/atuin.nix b/modules/programs/atuin.nix index b7e7f8808..073994316 100644 --- a/modules/programs/atuin.nix +++ b/modules/programs/atuin.nix @@ -145,11 +145,19 @@ in { (mkIf cfg.daemon.enable (mkMerge [ { - assertions = [{ - assertion = isLinux || isDarwin; - message = - "The atuin daemon can only be configured on either Linux or macOS."; - }]; + assertions = [ + { + assertion = versionAtLeast cfg.package.version "18.2.0"; + message = '' + The atuin daemon requires at least version 18.2.0 or later. + ''; + } + { + assertion = isLinux || isDarwin; + message = + "The atuin daemon can only be configured on either Linux or macOS."; + } + ]; programs.atuin.settings = { daemon = { enabled = true; }; }; } From f8bc330a13f80e13e70967bca0e674f711218ea2 Mon Sep 17 00:00:00 2001 From: Varun Narravula Date: Sat, 16 Nov 2024 13:12:25 -0800 Subject: [PATCH 4/5] atuin: capitalize mentions of "atuin" --- modules/programs/atuin.nix | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/programs/atuin.nix b/modules/programs/atuin.nix index 073994316..fe15cd252 100644 --- a/modules/programs/atuin.nix +++ b/modules/programs/atuin.nix @@ -149,13 +149,13 @@ in { { assertion = versionAtLeast cfg.package.version "18.2.0"; message = '' - The atuin daemon requires at least version 18.2.0 or later. + The Atuin daemon requires at least version 18.2.0 or later. ''; } { assertion = isLinux || isDarwin; message = - "The atuin daemon can only be configured on either Linux or macOS."; + "The Atuin daemon can only be configured on either Linux or macOS."; } ]; @@ -166,7 +166,7 @@ in { systemd.user.services.atuin-daemon = { Unit = { - Description = "atuin daemon"; + Description = "Atuin daemon"; Requires = [ "atuin-daemon.socket" ]; }; Install = { @@ -183,7 +183,7 @@ in { }; systemd.user.sockets.atuin-daemon = { - Unit = { Description = "atuin daemon socket"; }; + Unit = { Description = "Atuin daemon socket"; }; Install = { WantedBy = [ "sockets.target" ]; }; Socket = { ListenStream = "%h/.local/share/atuin/atuin.sock"; From dfdf59b2d539941aea5c26666c9ab809c1dc34df Mon Sep 17 00:00:00 2001 From: Varun Narravula Date: Sat, 16 Nov 2024 13:20:52 -0800 Subject: [PATCH 5/5] atuin: make daemon log level configurable --- modules/programs/atuin.nix | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/programs/atuin.nix b/modules/programs/atuin.nix index fe15cd252..269aa70b5 100644 --- a/modules/programs/atuin.nix +++ b/modules/programs/atuin.nix @@ -5,6 +5,7 @@ with lib; let cfg = config.programs.atuin; + daemonCfg = cfg.daemon; tomlFormat = pkgs.formats.toml { }; @@ -96,7 +97,18 @@ in { ''; }; - daemon.enable = mkEnableOption "atuin daemon"; + daemon = { + enable = mkEnableOption "Atuin daemon"; + + logLevel = mkOption { + default = null; + type = + types.nullOr (types.enum [ "trace" "debug" "info" "warn" "error" ]); + description = '' + Verbosity of Atuin daemon logging. + ''; + }; + }; }; config = let flagsStr = escapeShellArgs cfg.flags; @@ -143,7 +155,7 @@ in { }; } - (mkIf cfg.daemon.enable (mkMerge [ + (mkIf daemonCfg.enable (mkMerge [ { assertions = [ { @@ -175,7 +187,8 @@ in { }; Service = { ExecStart = "${lib.getExe cfg.package} daemon"; - Environment = [ "ATUIN_LOG=info" ]; + Environment = lib.optionals (daemonCfg.logLevel != null) + [ "ATUIN_LOG=${daemonCfg.logLevel}" ]; Restart = "on-failure"; RestartSteps = 3; RestartMaxDelaySec = 6; @@ -197,7 +210,10 @@ in { enable = true; config = { ProgramArguments = [ "${lib.getExe cfg.package}" "daemon" ]; - EnvironmentVariables = { ATUIN_LOG = "info"; }; + EnvironmentVariables = + lib.optionalAttrs (daemonCfg.logLevel != null) { + ATUIN_LOG = daemonCfg.logLevel; + }; KeepAlive = { Crashed = true; SuccessfulExit = false;