blob: 268c330ce9efdfbe65078455d3ca996302b66a93 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
{
config,
lib,
pkgs,
...
}: let
cfg = config.personal.nix;
in {
options.personal.nix = {
enable = lib.mkEnableOption "nix configuration";
autoUpgrade = {
enable = lib.mkEnableOption "automatic system and nixpkgs upgrade";
autoUpdateInputs = lib.mkOption {
type = with lib.types; listOf str;
default = ["nixpkgs"];
};
};
flake = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
gc.enable = lib.mkEnableOption "garbage collection";
};
config = lib.mkIf cfg.enable {
nixpkgs = {
config.allowUnfree = true;
flake = lib.mkDefault {
setNixPath = false;
setFlakeRegistry = false;
};
};
nix = {
package =
lib.getAttr (
if (builtins.compareVersions lib.trivial.version "23.11" > 0)
then "latest"
else "unstable"
)
pkgs.nixVersions;
settings = {
auto-optimise-store = true;
experimental-features = ["nix-command" "flakes"];
substituters = ["https://devenv.cachix.org/" "https://nix-community.cachix.org/"];
trusted-public-keys = ["devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=" "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="];
};
extraOptions = ''
!include secrets.conf
'';
gc = lib.mkIf cfg.gc.enable {
automatic = true;
dates = "daily";
options = "--delete-old";
};
};
system.autoUpgrade = lib.mkIf cfg.autoUpgrade.enable {
enable = true;
flake = cfg.flake;
flags = lib.optional (cfg.flake == null) "--upgrade-all";
};
systemd.services = lib.mkMerge [
(lib.mkIf cfg.autoUpgrade.enable {
# upgrading
flake-update = lib.mkIf (cfg.flake != null && cfg.autoUpgrade.autoUpdateInputs != []) {
preStart = "${pkgs.host}/bin/host firecat53.net"; # Check network connectivity
unitConfig = {
Description = "Update flake inputs";
StartLimitIntervalSec = 300;
StartLimitBurst = 5;
};
serviceConfig = {
ExecStart = "${config.nix.package}/bin/nix flake update --commit-lock-file --flake ${cfg.flake} " + lib.concatStringsSep " " cfg.autoUpgrade.autoUpdateInputs;
Restart = "on-failure";
RestartSec = "30";
Type = "oneshot"; # Ensure that it finishes before starting nixos-upgrade
};
before = ["nixos-upgrade.service"];
path = [pkgs.git];
personal.monitor = true;
};
nixos-upgrade = {
preStart = "${pkgs.host}/bin/host firecat53.net"; # Check network connectivity
serviceConfig = {
Restart = "on-failure";
RestartSec = "120";
};
unitConfig = {
StartLimitIntervalSec = 600;
StartLimitBurst = 2;
};
after = ["flake-update.service"];
wants = ["flake-update.service"];
personal.monitor = true;
};
})
{
# cleaning
nix-gc = {
after =
lib.optional (cfg.autoUpgrade.enable && cfg.gc.enable)
"nixos-upgrade.service";
personal.monitor = true;
};
nix-gc-remove-dead-roots = {
enable = cfg.gc.enable;
description = "Remove dead symlinks in /nix/var/nix/gcroots";
serviceConfig.Type = "oneshot";
script = "find /nix/var/nix/gcroots -xtype l -delete";
before = lib.mkIf config.nix.gc.automatic ["nix-gc.service"];
wantedBy = lib.mkIf config.nix.gc.automatic ["nix-gc.service"];
personal.monitor = true;
};
nix-gc-remove-old-hm-gens = let
user = config.personal.user;
in {
enable = cfg.gc.enable && user.enable && user.homeManager.enable;
description = "Remove old Home Manager generations for user ${user.name}";
serviceConfig.Type = "oneshot";
script = "${pkgs.nix}/bin/nix-env --profile /home/${user.name}/.local/state/nix/profiles/home-manager --delete-generations old";
before = lib.mkIf config.nix.gc.automatic ["nix-gc.service"];
wantedBy = lib.mkIf config.nix.gc.automatic ["nix-gc.service"];
personal.monitor = true;
};
}
];
programs.git = lib.mkIf (cfg.flake != null && lib.hasPrefix "git+file" cfg.flake) {
enable = true;
config.user = {
name = "Root user of ${config.networking.hostName}";
email = "root@${config.networking.hostName}";
};
};
};
}
|