From 398510f601cb1a1978a393814514f9ca9fbcfe72 Mon Sep 17 00:00:00 2001 From: Malo Bourgon Date: Mon, 25 Mar 2024 16:34:10 -0700 Subject: Add `nix.optimise` module --- modules/module-list.nix | 1 + modules/services/nix-optimise/default.nix | 73 +++++++++++++++++++++++++++++++ modules/system/checks.nix | 12 +++++ 3 files changed, 86 insertions(+) create mode 100644 modules/services/nix-optimise/default.nix (limited to 'modules') diff --git a/modules/module-list.nix b/modules/module-list.nix index 415ea49..e87f696 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -71,6 +71,7 @@ ./services/netbird.nix ./services/nix-daemon.nix ./services/nix-gc + ./services/nix-optimise ./services/ofborg ./services/postgresql ./services/privoxy diff --git a/modules/services/nix-optimise/default.nix b/modules/services/nix-optimise/default.nix new file mode 100644 index 0000000..5462bae --- /dev/null +++ b/modules/services/nix-optimise/default.nix @@ -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; + }; + }; + + }; +} diff --git a/modules/system/checks.nix b/modules/system/checks.nix index 27188e3..5989dc4 100644 --- a/modules/system/checks.nix +++ b/modules/system/checks.nix @@ -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) -- cgit v1.2.3