summaryrefslogtreecommitdiff
path: root/sys/default.nix
blob: ae1b38fc0fedc6f8eb436978b67b702701de4d16 (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
{ self }:
{ lib, config, pkgs, modulesPath, ... }:
with lib; let
  cfg = config.local;
in {
  imports = [
    "${modulesPath}/installer/scan/not-detected.nix"
    ./auth.nix
    ./fs
    ./options.nix
    ./users.nix
  ];

  config = {
    nixpkgs.overlays = [ self.overlay ];

    # This value determines the NixOS release from which the default
    # settings for stateful data, like file locations and database versions
    # on your system were taken. It‘s perfectly fine and recommended to leave
    # this value at the release version of the first install of this system.
    # Before changing this value read the documentation for this option
    # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
    system.stateVersion = "21.11"; # Did you read the comment?

    nix = {
      package = pkgs.nixFlakes;
      extraOptions = ''
        experimental-features = nix-command flakes
      '';
    };

    boot = {
      loader = (if cfg.loader == "grub" then {
        grub = {
          enable = true;
          device = "nodev";
          efiSupport = true;
        };
      } else {
        systemd-boot.enable = true;
      }) // {
        efi = {
          inherit (cfg) canTouchEfiVariables;
        };
      };

      initrd = let
        crypt = cfg.crypt.toplevel;
        headerPathEscaped = escapeShellArg "/initrd-boot/${crypt.headerFromBoot}";
      in {
        availableKernelModules = cfg.initrdModules;
        supportedFilesystems = [ "vfat" ];

        preDeviceCommands = optionalString (crypt != null) ''
          mkdir -p `dirname ${headerPathEscaped}`
          touch ${headerPathEscaped}
        '';
 
        preLVMCommands = optionalString cfg.portable ''
          sleep 2 #TODO
        '';

        postMountCommands = let
          fromRoot = path: escapeShellArg "/mnt-root/${path}";
          auxOpen = aux: ''
            cryptsetup -v open \
              --header ${fromRoot aux.header} \
              --key-file ${fromRoot aux.keyfile} \
              ${aux.device} ${aux.target}
          '';
        in concatStringsSep "\n" (map auxOpen cfg.crypt.aux);

        luks.devices = mkIf (crypt != null) {
          "${crypt.target}" = {
            inherit (crypt) device;
            header = "/initrd-boot/${crypt.headerFromBoot}";
            preLVM = false;

            preOpenCommands = ''
              mount -o ro -t vfat ${escapeShellArg cfg.fs.boot.device} /initrd-boot
            '';

            postOpenCommands = ''
              umount /initrd-boot
            '';
          };
        };

        #network = {
        #  enable = true;

        #  ssh = {
        #    enable = true;
        #    port = 2234;
        #  };
        #};
      };
    };

    hardware.cpu = let
      ucode.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
    in {
      amd = mkIf (cfg.cpuVendor == "amd") ucode;
      intel = mkIf (cfg.cpuVendor == "intel") ucode;
    };

    time.timeZone = "America/Costa_Rica";
    i18n.defaultLocale = "es_CR.UTF-8";

    networking = {
      hostName = cfg.hostname;
      useDHCP = false;
      useNetworkd = true;

      interfaces = mkIf (cfg.dhcpInterface != null) {
        "${cfg.dhcpInterface}".useDHCP = true;
      };

      wireguard.enable = true;
    };

    environment.systemPackages = [ pkgs.dhcpcd pkgs.git ];
  };
}