diff options
Diffstat (limited to 'nix')
| -rw-r--r-- | nix/module.hm.nix | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/nix/module.hm.nix b/nix/module.hm.nix new file mode 100644 index 00000000..1891abb6 --- /dev/null +++ b/nix/module.hm.nix @@ -0,0 +1,129 @@ +{turtlePkg}: { + config, + pkgs, + lib, + ... +}: let + cfg = config.programs.turtle; + + tomlFormat = pkgs.formats.toml {}; + + inherit + (lib) + mkIf + mkOption + types + ; +in { + options.programs.turtle = { + enable = lib.mkEnableOption "turtle"; + + package = (lib.mkPackageOption pkgs "turtle" {}) // {default = turtlePkg;}; + + settings = mkOption { + type = with types; let + prim = oneOf [ + bool + int + str + ]; + primOrPrimAttrs = either prim (attrsOf prim); + entry = either prim (listOf primOrPrimAttrs); + entryOrAttrsOf = t: either entry (attrsOf t); + entries = entryOrAttrsOf (entryOrAttrsOf entry); + in + attrsOf entries // {description = "Turtle configuration";}; + default = {}; + example = lib.literalExpression '' + { + auto_sync = true; + sync_frequency = "5m"; + sync_address = "https://api.atuin.sh"; + search_mode = "prefix"; + } + ''; + description = '' + Configuration written to + {file}`$XDG_CONFIG_HOME/turtle/config.toml`. + ''; + }; + + daemon = { + logLevel = mkOption { + default = null; + type = types.nullOr ( + types.enum [ + "trace" + "debug" + "info" + "warn" + "error" + ] + ); + description = '' + Verbosity of Turtle daemon logging. + ''; + }; + }; + }; + + config = mkIf cfg.enable { + assertions = [ + { + assertion = config.systemd.user.enable || config.launchd.enable; + message = "The Turtle daemon can only be configured on systems with systemd or launchd."; + } + ]; + + # Always add the configured `turtle` package. + home.packages = [cfg.package]; + + # If there are user-provided settings, generate the config file. + xdg.configFile = lib.mkMerge [ + (mkIf (cfg.settings != {}) { + "turtle/config.toml" = { + source = tomlFormat.generate "turtle-config" cfg.settings; + }; + }) + ]; + + programs.turtle.settings.daemon = { + systemd_socket = config.systemd.user.enable; + socket_path = lib.mkIf (!config.systemd.user.enable) ( + lib.mkDefault "${config.xdg.dataHome}/turtle/daemon.sock" + ); + }; + + systemd.user.services.turtle-daemon = { + Unit = { + Description = "Turtle daemon"; + Requires = ["turtle-daemon.socket"]; + }; + Install = { + Also = ["turtle-daemon.socket"]; + WantedBy = ["default.target"]; + }; + Service = { + ExecStart = "${lib.getExe' cfg.package "turtle-daemon"} start"; + Environment = lib.optionals (cfg.daemon.logLevel != null) ["ATUIN_LOG=${cfg.daemon.logLevel}"]; + Restart = "on-failure"; + RestartSteps = 3; + RestartMaxDelaySec = 6; + }; + }; + + systemd.user.sockets.turtle-daemon = { + Unit = { + Description = "Turtle daemon socket"; + }; + Install = { + WantedBy = ["sockets.target"]; + }; + Socket = { + ListenStream = "%t/turtle.sock"; + SocketMode = "0600"; + RemoveOnStop = true; + }; + }; + }; +} |
