nh: fix clean option behaviour for Darwin

This commit is contained in:
Ali Jamadi 2025-06-30 16:52:30 -06:00 committed by Austin Horstman
parent 78fc50f1cf
commit e8a3e2c1e0
6 changed files with 77 additions and 5 deletions

View file

@ -37,7 +37,9 @@ in
description = ''
How often cleanup is performed.
The format is described in {manpage}`systemd.time(7)`.
On linux the format is described in {manpage}`systemd.time(7)`.
${lib.hm.darwin.intervalDocumentation}
'';
};
@ -59,6 +61,10 @@ in
lib.optional (cfg.clean.enable && config.nix.gc.automatic)
"programs.nh.clean.enable and nix.gc.automatic (Home-Manager) are both enabled. Please use one or the other to avoid conflict.";
assertions = lib.optionals pkgs.stdenv.isDarwin [
(lib.hm.darwin.assertInterval "programs.nh.clean.dates" cfg.clean.dates pkgs)
];
home = lib.mkIf cfg.enable {
packages = [ cfg.package ];
sessionVariables = lib.mkIf (cfg.flake != null) (
@ -70,7 +76,7 @@ in
);
};
systemd.user = lib.mkIf cfg.clean.enable {
systemd.user = lib.mkIf (cfg.clean.enable && pkgs.stdenv.isLinux) {
services.nh-clean = {
Unit.Description = "Nh clean (user)";
@ -90,6 +96,21 @@ in
Install.WantedBy = [ "timers.target" ];
};
};
launchd.agents.nh-clean = lib.mkIf (cfg.clean.enable && pkgs.stdenv.isDarwin) {
enable = true;
config = {
ProgramArguments = [
"${lib.getExe cfg.package}"
"clean"
"user"
] ++ lib.optional (cfg.clean.extraArgs != "") cfg.clean.extraArgs;
StartCalendarInterval = lib.hm.darwin.mkCalendarInterval cfg.clean.dates;
};
};
};
}

View file

@ -101,6 +101,7 @@ let
"neovide"
"neovim"
"newsboat"
"nh"
"nheko"
"nix"
"nix-direnv"

View file

@ -0,0 +1,22 @@
{
programs.nh = {
enable = true;
flake = "/path/to/flake";
clean = {
enable = true;
dates = "weekly";
};
};
nmt.script = ''
serviceFile="LaunchAgents/org.nix-community.home.nh-clean.plist"
serviceFileNormalized="$(normalizeStorePaths "$serviceFile")"
assertFileExists $serviceFile
assertFileContent $serviceFileNormalized ${./launchd.plist}
assertFileRegex home-path/etc/profile.d/hm-session-vars.sh 'NH_FLAKE="/path/to/flake"'
'';
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.nix-community.home.nh-clean</string>
<key>ProgramArguments</key>
<array>
<string>@nh@/bin/nh</string>
<string>clean</string>
<string>user</string>
</array>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>0</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>1</integer>
</dict>
</array>
</dict>
</plist>

View file

@ -1,4 +1,7 @@
{ lib, pkgs, ... }:
lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
nh = ./nh.nix;
}
(lib.optionalAttrs pkgs.stdenv.hostPlatform.isDarwin {
nh = ./darwin/config.nix;
})
// (lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
nh = ./linux/config.nix;
})