From 879c2dd588d42e6c09bec256a09d262c7abdce9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Sat, 20 May 2023 02:24:32 +0200 Subject: [PATCH 1/2] nixos/nix-optimise: cleanup, remove with lib --- nixos/modules/services/misc/nix-optimise.nix | 34 +++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/nixos/modules/services/misc/nix-optimise.nix b/nixos/modules/services/misc/nix-optimise.nix index db8148c060e7..58fc1ea4162a 100644 --- a/nixos/modules/services/misc/nix-optimise.nix +++ b/nixos/modules/services/misc/nix-optimise.nix @@ -1,28 +1,21 @@ { config, lib, ... }: -with lib; - let cfg = config.nix.optimise; in { - - ###### interface - options = { - nix.optimise = { - - automatic = mkOption { + automatic = lib.mkOption { default = false; - type = types.bool; + type = lib.types.bool; description = lib.mdDoc "Automatically run the nix store optimiser at a specific time."; }; - dates = mkOption { + dates = lib.mkOption { default = ["03:45"]; - type = types.listOf types.str; + type = with lib.types; listOf str; description = lib.mdDoc '' Specification (in the format described by {manpage}`systemd.time(7)`) of the time at @@ -32,9 +25,6 @@ in }; }; - - ###### implementation - config = { assertions = [ { @@ -43,14 +33,12 @@ in } ]; - systemd.services.nix-optimise = lib.mkIf config.nix.enable - { description = "Nix Store Optimiser"; - # No point this if the nix daemon (and thus the nix store) is outside - unitConfig.ConditionPathIsReadWrite = "/nix/var/nix/daemon-socket"; - serviceConfig.ExecStart = "${config.nix.package}/bin/nix-store --optimise"; - startAt = optionals cfg.automatic cfg.dates; - }; - + systemd.services.nix-optimise = lib.mkIf config.nix.enable { + description = "Nix Store Optimiser"; + # No point this if the nix daemon (and thus the nix store) is outside + unitConfig.ConditionPathIsReadWrite = "/nix/var/nix/daemon-socket"; + serviceConfig.ExecStart = "${config.nix.package}/bin/nix-store --optimise"; + startAt = lib.optionals cfg.automatic cfg.dates; + }; }; - } From f2ea5c05c14e7c8ea962058f3028c2aca45993cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Sat, 20 May 2023 02:32:19 +0200 Subject: [PATCH 2/2] nixos/nix-optimise: persist timer otherwise the timer might never run on laptops which could be shutdown during the night --- nixos/modules/services/misc/nix-optimise.nix | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/nixos/modules/services/misc/nix-optimise.nix b/nixos/modules/services/misc/nix-optimise.nix index 58fc1ea4162a..0398229a13da 100644 --- a/nixos/modules/services/misc/nix-optimise.nix +++ b/nixos/modules/services/misc/nix-optimise.nix @@ -33,12 +33,19 @@ in } ]; - systemd.services.nix-optimise = lib.mkIf config.nix.enable { - description = "Nix Store Optimiser"; - # No point this if the nix daemon (and thus the nix store) is outside - unitConfig.ConditionPathIsReadWrite = "/nix/var/nix/daemon-socket"; - serviceConfig.ExecStart = "${config.nix.package}/bin/nix-store --optimise"; - startAt = lib.optionals cfg.automatic cfg.dates; + systemd = lib.mkIf config.nix.enable { + services.nix-optimise = { + description = "Nix Store Optimiser"; + # No point this if the nix daemon (and thus the nix store) is outside + unitConfig.ConditionPathIsReadWrite = "/nix/var/nix/daemon-socket"; + serviceConfig.ExecStart = "${config.nix.package}/bin/nix-store --optimise"; + startAt = lib.optionals cfg.automatic cfg.dates; + }; + + timers.nix-optimise.timerConfig = { + Persistent = true; + RandomizedDelaySec = 1800; + }; }; }; }