about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/tasks/swraid.nix
blob: f624294565b0614b2406a0627562758066b93500 (plain) (blame)
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
{ config, pkgs, lib, ... }: let

  cfg = config.boot.swraid;

in {
  imports = [
    (lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "enable" ] [ "boot" "swraid" "enable" ])
    (lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "mdadmConf" ] [ "boot" "swraid" "mdadmConf" ])
  ];


  options.boot.swraid = {
    enable = lib.mkEnableOption (lib.mdDoc "swraid support using mdadm") // {
      description = lib.mdDoc ''
        Whether to enable support for Linux MD RAID arrays.

        When this is enabled, mdadm will be added to the system path,
        and MD RAID arrays will be detected and activated
        automatically, both in stage-1 (initramfs) and in stage-2 (the
        final NixOS system).

        This should be enabled if you want to be able to access and/or
        boot from MD RAID arrays. {command}`nixos-generate-config`
        should detect it correctly in the standard installation
        procedure.
      '';
      default = lib.versionOlder config.system.stateVersion "23.11";
      defaultText = lib.mdDoc "`true` if stateVersion is older than 23.11";
    };

    mdadmConf = lib.mkOption {
      description = lib.mdDoc "Contents of {file}`/etc/mdadm.conf`.";
      type = lib.types.lines;
      default = "";
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ pkgs.mdadm ];

    services.udev.packages = [ pkgs.mdadm ];

    systemd.packages = [ pkgs.mdadm ];

    boot.initrd = {
      availableKernelModules = [ "md_mod" "raid0" "raid1" "raid10" "raid456" ];

      extraUdevRulesCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
        cp -v ${pkgs.mdadm}/lib/udev/rules.d/*.rules $out/
      '';

      extraUtilsCommands = ''
        # Add RAID mdadm tool.
        copy_bin_and_libs ${pkgs.mdadm}/sbin/mdadm
        copy_bin_and_libs ${pkgs.mdadm}/sbin/mdmon
      '';

      extraUtilsCommandsTest = ''
        $out/bin/mdadm --version
      '';

      extraFiles."/etc/mdadm.conf".source = pkgs.writeText "mdadm.conf" config.boot.swraid.mdadmConf;

      systemd = {
        contents."/etc/mdadm.conf" = lib.mkIf (cfg.mdadmConf != "") {
          text = cfg.mdadmConf;
        };

        packages = [ pkgs.mdadm ];
        initrdBin = [ pkgs.mdadm ];
      };

      services.udev.packages = [ pkgs.mdadm ];
    };
  };
}