From 2ab55ebf384fca29dd1e73d882746f0340054e9b Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Thu, 2 Jan 2025 14:18:32 -0600 Subject: [PATCH] feat(hosts/orion): integrate `optimize-nix-store` module --- flake.nix | 1 + hosts/orion/modules/nix.nix | 7 +++- modules/optimize-nix-store.nix | 67 ++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 modules/optimize-nix-store.nix diff --git a/flake.nix b/flake.nix index 53cd7f83..970b9e90 100644 --- a/flake.nix +++ b/flake.nix @@ -162,6 +162,7 @@ }; modules = [ ./modules/btrfs-rollback.nix + ./modules/optimize-nix-store.nix inputs.home-manager.nixosModules.home-manager { home-manager = { diff --git a/hosts/orion/modules/nix.nix b/hosts/orion/modules/nix.nix index a8fdadc1..d732c3bf 100644 --- a/hosts/orion/modules/nix.nix +++ b/hosts/orion/modules/nix.nix @@ -3,6 +3,10 @@ { nixpkgs.config.allowUnfree = true; nix = { + optimize-nix-store = { + enable = true; + randomizedDelaySec = "30min"; + }; nixPath = [ "nixpkgs=${inputs.nixpkgs}" ]; settings = { experimental-features = [ @@ -10,7 +14,6 @@ "nix-command" "flakes" ]; - auto-optimise-store = true; use-xdg-base-directories = true; trusted-users = [ "@wheel" ]; substituters = [ @@ -24,7 +27,7 @@ }; gc = { automatic = true; - dates = "weekly"; + dates = "daily"; options = "--delete-older-than 7d"; }; }; diff --git a/modules/optimize-nix-store.nix b/modules/optimize-nix-store.nix new file mode 100644 index 00000000..e906896a --- /dev/null +++ b/modules/optimize-nix-store.nix @@ -0,0 +1,67 @@ +{ + config, + lib, + ... +}: +let + cfg = config.nix.optimize-nix-store; +in +{ + options.nix.optimize-nix-store = { + enable = lib.mkEnableOption "Auto optimize the nix store on a schedule."; + + dates = lib.mkOption { + type = lib.types.singleLineStr; + default = "daily"; + example = "weekly"; + description = '' + How often or when nix store optimization is performed. + + This value must be a calendar event in the format specified by + {manpage}`systemd.time(7)`. + ''; + }; + + randomizedDelaySec = lib.mkOption { + default = "0"; + type = lib.types.singleLineStr; + example = "45min"; + description = '' + Add a randomized delay before each nix store optimization. + The delay will be chosen between zero and this value. + This value must be a time span in the format specified by + {manpage}`systemd.time(7)` + ''; + }; + + persistent = lib.mkOption { + default = true; + type = lib.types.bool; + example = false; + description = '' + Takes a boolean argument. If true, the time when the service + unit was last triggered is stored on disk. When the timer is + activated, the service unit is triggered immediately if it + would have been triggered at least once during the time when + the timer was inactive. Such triggering is nonetheless + subject to the delay imposed by RandomizedDelaySec=. This is + useful to catch up on missed runs of the service when the + system was powered down. + ''; + }; + }; + config = lib.mkIf (cfg.enable && config.nix.enable) { + systemd = { + services.optimize-nix-store = { + description = "Optimizes the Nix Store on a regular schedule"; + script = "exec ${config.nix.package.out}/bin/nix-store --optimise"; + serviceConfig.Type = "oneshot"; + startAt = cfg.dates; + }; + timers.optimize-nix-store.timerConfig = { + Persistent = cfg.persistent; + RandomizedDelaySec = cfg.randomizedDelaySec; + }; + }; + }; +}