1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-12-14 11:57:34 +00:00

Merge pull request #915 from malob/add-nix-optimise-module

Add `nix.optimise` module
This commit is contained in:
Michael Hoang 2024-03-30 12:48:46 +11:00 committed by GitHub
commit 36524adc31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 112 additions and 0 deletions

View file

@ -71,6 +71,7 @@
./services/netbird.nix
./services/nix-daemon.nix
./services/nix-gc
./services/nix-optimise
./services/ofborg
./services/postgresql
./services/privoxy

View file

@ -0,0 +1,73 @@
# Based off:
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/nix-optimise.nix
# When making changes please try to keep it in sync.
{ config, lib, ... }:
let
inherit (lib)
mdDoc
mkIf
mkOption
mkRemovedOptionModule
optionalString
types
;
cfg = config.nix.optimise;
in
{
imports = [
(mkRemovedOptionModule [ "nix" "optimise" "dates" ] "Use `nix.optimise.interval` instead.")
];
###### interface
options = {
nix.optimise = {
automatic = mkOption {
type = types.bool;
default = false;
description = mdDoc "Automatically run the nix store optimiser at a specific time.";
};
# Not in NixOS module
user = mkOption {
type = types.nullOr types.str;
default = null;
description = mdDoc "User that runs the store optimisation.";
};
interval = mkOption {
type = types.attrs;
default = { Hour = 3; Minute = 15; };
description = mdDoc "The time interval at which the optimiser will run.";
};
};
};
###### implementation
config = mkIf cfg.automatic {
launchd.daemons.nix-optimise = {
environment.NIX_REMOTE = optionalString config.nix.useDaemon "daemon";
serviceConfig = {
ProgramArguments = [
"/bin/sh" "-c"
"/bin/wait4path ${config.nix.package} && exec ${config.nix.package}/bin/nix-store --optimise"
];
RunAtLoad = false;
StartCalendarInterval = [ cfg.interval ];
UserName = cfg.user;
};
};
};
}

View file

@ -191,6 +191,17 @@ let
exit 2
fi
'';
nixStoreOptimiser = ''
if test -O /nix/store; then
echo "error: A single-user install can't run optimiser as root, aborting activation" >&2
echo "Configure the optimiser to run as the current user:" >&2
echo >&2
echo " nix.optimiser.user = \"$USER\";" >&2
echo >&2
exit 2
fi
'';
in
{
@ -230,6 +241,7 @@ in
(mkIf (!config.nix.useDaemon) singleUser)
nixStore
(mkIf (config.nix.gc.automatic && config.nix.gc.user == null) nixGarbageCollector)
(mkIf (config.nix.optimise.automatic && config.nix.optimise.user == null) nixStoreOptimiser)
(mkIf cfg.verifyNixChannels nixChannels)
nixInstaller
(mkIf cfg.verifyNixPath nixPath)

View file

@ -126,6 +126,7 @@ let
tests.services-dnsmasq = makeTest ./tests/services-dnsmasq.nix;
tests.services-eternal-terminal = makeTest ./tests/services-eternal-terminal.nix;
tests.services-nix-gc = makeTest ./tests/services-nix-gc.nix;
tests.services-nix-optimise = makeTest ./tests/services-nix-optimise.nix;
tests.services-nextdns = makeTest ./tests/services-nextdns.nix;
tests.services-ofborg = makeTest ./tests/services-ofborg.nix;
tests.services-offlineimap = makeTest ./tests/services-offlineimap.nix;

View file

@ -0,0 +1,25 @@
{ config, pkgs, ... }:
let
nix = pkgs.runCommand "nix-2.2" {} "mkdir -p $out";
in
{
nix.optimise.automatic = true;
nix.optimise.user = "nixuser";
nix.package = nix;
test = ''
echo checking nix-optimise service in /Library/LaunchDaemons >&2
grep "<string>org.nixos.nix-optimise</string>" \
${config.out}/Library/LaunchDaemons/org.nixos.nix-optimise.plist
grep "<string>/bin/wait4path ${nix} &amp;&amp; exec ${nix}/bin/nix-store --optimise</string>" \
${config.out}/Library/LaunchDaemons/org.nixos.nix-optimise.plist
grep "<key>UserName</key>" ${config.out}/Library/LaunchDaemons/org.nixos.nix-optimise.plist
grep "<string>nixuser</string>" ${config.out}/Library/LaunchDaemons/org.nixos.nix-optimise.plist
(! grep "<key>KeepAlive</key>" ${config.out}/Library/LaunchDaemons/org.nixos.nix-optimise.plist)
echo checking nix-optimise validation >&2
(! grep "nix.optimise.user = " ${config.out}/activate-user)
'';
}