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