From f7f86011f593640ef6a98426f63aa94a9dce6288 Mon Sep 17 00:00:00 2001 From: John Titor <50095635+JohnRTitor@users.noreply.github.com> Date: Tue, 23 Apr 2024 23:07:08 +0530 Subject: [PATCH] nh: add module Add nh to news --- modules/misc/news.nix | 8 ++++ modules/modules.nix | 1 + modules/programs/nh.nix | 84 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 modules/programs/nh.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index bef867bb..14a81d91 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1703,6 +1703,14 @@ in { one place. See https://github.com/glanceapp/glance for more. ''; } + + { + time = "2024-04-23T18:50:00+00:00"; + condition = hostPlatform.isLinux; + message = '' + A new module is available: 'programs.nh'. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index dbeebfbf..c79ffb39 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -169,6 +169,7 @@ let ./programs/neomutt.nix ./programs/neovim.nix ./programs/newsboat.nix + ./programs/nh.nix ./programs/nheko.nix ./programs/nix-index.nix ./programs/nnn.nix diff --git a/modules/programs/nh.nix b/modules/programs/nh.nix new file mode 100644 index 00000000..58c9af62 --- /dev/null +++ b/modules/programs/nh.nix @@ -0,0 +1,84 @@ +{ config, osConfig, lib, pkgs, ... }: + +let cfg = config.programs.nh; +in { + meta.maintainers = with lib.maintainers; [ johnrtitor ]; + + options.programs.nh = { + enable = lib.mkEnableOption "nh, yet another Nix CLI helper"; + + package = lib.mkPackageOption pkgs "nh" { }; + + flake = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + description = '' + The path that will be used for the `FLAKE` environment variable. + + `FLAKE` is used by nh as the default flake for performing actions, like `nh os switch`. + ''; + }; + + clean = { + enable = lib.mkEnableOption + "periodic garbage collection for user profile and nix store with nh clean user"; + + dates = lib.mkOption { + type = lib.types.singleLineStr; + default = "weekly"; + description = '' + How often cleanup is performed. Passed to systemd.time + + The format is described in + {manpage}`systemd.time(7)`. + ''; + }; + + extraArgs = lib.mkOption { + type = lib.types.singleLineStr; + default = ""; + example = "--keep 5 --keep-since 3d"; + description = '' + Options given to nh clean when the service is run automatically. + + See `nh clean all --help` for more information. + ''; + }; + }; + }; + + config = { + warnings = lib.optionals (!(cfg.clean.enable -> !osConfig.nix.gc.automatic)) + [ + "programs.nh.clean.enable and nix.gc.automatic (system-wide in configuration.nix) are both enabled. Please use one or the other to avoid conflict." + ]; + + assertions = [{ + assertion = (cfg.flake != null) -> !(lib.hasSuffix ".nix" cfg.flake); + message = "nh.flake must be a directory, not a nix file"; + }]; + + home = lib.mkIf cfg.enable { + packages = [ cfg.package ]; + sessionVariables = lib.mkIf (cfg.flake != null) { FLAKE = cfg.flake; }; + }; + + systemd.user = lib.mkIf cfg.clean.enable { + services.nh-clean = { + Unit.Description = "Nh clean (user)"; + + Service = { + Type = "oneshot"; + ExecStart = "exec ${lib.getExe cfg.package} clean user ${cfg.clean.extraArgs}"; + Environment = "PATH=$PATH:${config.nix.package}"; + }; + }; + timers.nh-clean = { + Timer = { + OnCalendar = cfg.clean.dates; + Persistent = true; + }; + }; + }; + }; +}