1{ config, lib, ... }:
2
3let
4 cfg = config.nix.optimise;
5in
6
7{
8 options = {
9 nix.optimise = {
10 automatic = lib.mkOption {
11 default = false;
12 type = lib.types.bool;
13 description = "Automatically run the nix store optimiser at a specific time.";
14 };
15
16 dates = lib.mkOption {
17 default = ["03:45"];
18 type = with lib.types; listOf str;
19 description = ''
20 Specification (in the format described by
21 {manpage}`systemd.time(7)`) of the time at
22 which the optimiser will run.
23 '';
24 };
25 };
26 };
27
28 config = {
29 assertions = [
30 {
31 assertion = cfg.automatic -> config.nix.enable;
32 message = ''nix.optimise.automatic requires nix.enable'';
33 }
34 ];
35
36 systemd = lib.mkIf config.nix.enable {
37 services.nix-optimise = {
38 description = "Nix Store Optimiser";
39 # No point this if the nix daemon (and thus the nix store) is outside
40 unitConfig.ConditionPathIsReadWrite = "/nix/var/nix/daemon-socket";
41 serviceConfig.ExecStart = "${config.nix.package}/bin/nix-store --optimise";
42 startAt = lib.optionals cfg.automatic cfg.dates;
43 };
44
45 timers.nix-optimise = lib.mkIf cfg.automatic {
46 timerConfig = {
47 Persistent = true;
48 RandomizedDelaySec = 1800;
49 };
50 };
51 };
52 };
53}