Created
July 21, 2026 15:14
-
-
Save nightuser/42c93ef4059372fc8386a7b5cebb89e7 to your computer and use it in GitHub Desktop.
NH module for nix-darwin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Based on: | |
| # * https://github.com/NixOS/nixpkgs/blob/nixos-26.05/nixos/modules/programs/nh.nix | |
| # * https://github.com/nix-community/home-manager/blob/release-26.05/modules/programs/nh.nix | |
| # * https://github.com/nix-darwin/nix-darwin/blob/nix-darwin-26.05/modules/services/nix-gc/default.nix | |
| { | |
| config, | |
| lib, | |
| pkgs, | |
| modulesPath, | |
| ... | |
| }: | |
| let | |
| cfg = config.programs.nh; | |
| launchdTypes = import (modulesPath + "/launchd/types.nix") { inherit config lib; }; | |
| in | |
| { | |
| 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.str; | |
| default = null; | |
| description = '' | |
| The string that will be used for the {env}`NH_FLAKE` environment variable. | |
| ''; | |
| }; | |
| darwinFlake = lib.mkOption { | |
| type = lib.types.nullOr lib.types.str; | |
| default = null; | |
| description = '' | |
| The string that will be used for the {env}`NH_DARWIN_FLAKE` environment variable. | |
| {env}`NH_DARWIN_FLAKE` is used by nh as the default flake for performing | |
| {command}`nh darwin` actions, such as {command}`nh darwin switch`. | |
| Setting this will take priority over the `flake` option. | |
| ''; | |
| }; | |
| clean = { | |
| enable = lib.mkEnableOption "periodic garbage collection with nh clean all"; | |
| interval = lib.mkOption { | |
| type = launchdTypes.StartCalendarInterval; | |
| default = [ | |
| { | |
| Weekday = 7; | |
| Hour = 3; | |
| Minute = 15; | |
| } | |
| ]; | |
| description = '' | |
| The calendar interval at which the garbage collector will run. | |
| See the {option}`serviceConfig.StartCalendarInterval` option of | |
| the {option}`launchd` module for more info. | |
| ''; | |
| }; | |
| 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 {command}`nh clean all --help` for more information. | |
| ''; | |
| }; | |
| }; | |
| }; | |
| config = { | |
| warnings = | |
| if (!(cfg.clean.enable -> !config.nix.gc.automatic)) then | |
| [ | |
| "programs.nh.clean.enable and nix.gc.automatic are both enabled. Please use one or the other to avoid conflict." | |
| ] | |
| else | |
| [ ]; | |
| environment = lib.mkIf cfg.enable { | |
| systemPackages = [ cfg.package ]; | |
| variables = lib.mkMerge [ | |
| (lib.mkIf (cfg.flake != null) { | |
| NH_FLAKE = cfg.flake; | |
| }) | |
| (lib.mkIf (cfg.darwinFlake != null) { | |
| NH_DARWIN_FLAKE = cfg.darwinFlake; | |
| }) | |
| ]; | |
| }; | |
| launchd.daemons.nh-clean = lib.mkIf cfg.clean.enable { | |
| command = "${lib.getExe cfg.package} clean all ${cfg.clean.extraArgs}"; | |
| serviceConfig = { | |
| RunAtLoad = false; | |
| StartCalendarInterval = cfg.clean.interval; | |
| }; | |
| path = [ config.nix.package ]; | |
| }; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment