summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorMalo Bourgon <mbourgon@gmail.com>2024-03-25 16:34:10 -0700
committerMalo Bourgon <mbourgon@gmail.com>2024-03-27 12:50:04 -0700
commit398510f601cb1a1978a393814514f9ca9fbcfe72 (patch)
tree5fbf5ed358041c70c9f50419b2e35504b9eff856 /modules
parentbcc8afd06e237df060c85bad6af7128e05fd61a3 (diff)
Add `nix.optimise` module
Diffstat (limited to 'modules')
-rw-r--r--modules/module-list.nix1
-rw-r--r--modules/services/nix-optimise/default.nix73
-rw-r--r--modules/system/checks.nix12
3 files changed, 86 insertions, 0 deletions
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} &amp;&amp; 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)