summaryrefslogtreecommitdiff
path: root/modules/nixos/personal/nix.nix
blob: 11b1c17f0c4a02f65241de177226c5e7afc52d97 (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
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.personal.nix;
in {
  options.personal.nix = {
    enable = lib.mkEnableOption "nix configuration";
    autoUpgrade = lib.mkEnableOption "automatic system and nixpkgs upgrade";
    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;};
    environment.etc."nix/registry.json".text = lib.mkForce (builtins.toJSON {
      version = 2;
    });
    nix = {
      package = pkgs.nixVersions.unstable;
      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="];
      };
      gc = lib.mkIf cfg.gc.enable {
        automatic = true;
        dates = "daily";
        options = "--delete-old";
      };
    };
    system.autoUpgrade = lib.mkIf cfg.autoUpgrade {
      enable = true;
      flake = cfg.flake;
      flags =
        if (cfg.flake == null)
        then ["--upgrade-all"]
        else ["--commit-lock-file"] ++ pkgs.personal.lib.updateInputFlag "nixpkgs";
    };
    systemd.services = {
      nixos-upgrade.personal.monitor = true;
      nix-gc = {
        after =
          lib.optional (cfg.autoUpgrade && 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}";
      };
    };
  };
}