1{ config, pkgs, lib, ... }: let
2
3 cfg = config.boot.swraid;
4
5 mdadm_conf = config.environment.etc."mdadm.conf";
6
7 enable_implicitly_for_old_state_versions = lib.versionOlder config.system.stateVersion "23.11";
8
9 minimum_config_is_set = config_text:
10 (builtins.match ".*(MAILADDR|PROGRAM).*" mdadm_conf.text) != null;
11
12in {
13 imports = [
14 (lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "enable" ] [ "boot" "swraid" "enable" ])
15 (lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "mdadmConf" ] [ "boot" "swraid" "mdadmConf" ])
16 ];
17
18
19 options.boot.swraid = {
20 enable = lib.mkEnableOption "swraid support using mdadm" // {
21 description = ''
22 Whether to enable support for Linux MD RAID arrays.
23
24 When this is enabled, mdadm will be added to the system path,
25 and MD RAID arrays will be detected and activated
26 automatically, both in stage-1 (initramfs) and in stage-2 (the
27 final NixOS system).
28
29 This should be enabled if you want to be able to access and/or
30 boot from MD RAID arrays. {command}`nixos-generate-config`
31 should detect it correctly in the standard installation
32 procedure.
33 '';
34 default = enable_implicitly_for_old_state_versions;
35 defaultText = "`true` if stateVersion is older than 23.11";
36 };
37
38 mdadmConf = lib.mkOption {
39 description = "Contents of {file}`/etc/mdadm.conf`.";
40 type = lib.types.lines;
41 default = "";
42 };
43 };
44
45 config = lib.mkIf cfg.enable {
46 warnings = lib.mkIf
47 ( !enable_implicitly_for_old_state_versions && !minimum_config_is_set mdadm_conf)
48 [ "mdadm: Neither MAILADDR nor PROGRAM has been set. This will cause the `mdmon` service to crash." ];
49
50 environment.systemPackages = [ pkgs.mdadm ];
51
52 environment.etc."mdadm.conf".text = lib.mkAfter cfg.mdadmConf;
53
54 services.udev.packages = [ pkgs.mdadm ];
55
56 systemd.packages = [ pkgs.mdadm ];
57
58 boot.initrd = {
59 availableKernelModules = [ "md_mod" "raid0" "raid1" "raid10" "raid456" ];
60
61 extraUdevRulesCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
62 cp -v ${pkgs.mdadm}/lib/udev/rules.d/*.rules $out/
63 '';
64
65 extraUtilsCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
66 # Add RAID mdadm tool.
67 copy_bin_and_libs ${pkgs.mdadm}/sbin/mdadm
68 copy_bin_and_libs ${pkgs.mdadm}/sbin/mdmon
69 '';
70
71 extraUtilsCommandsTest = lib.mkIf (!config.boot.initrd.systemd.enable) ''
72 $out/bin/mdadm --version
73 '';
74
75 extraFiles."/etc/mdadm.conf".source = pkgs.writeText "mdadm.conf" mdadm_conf.text;
76
77 systemd = {
78 contents."/etc/mdadm.conf".text = mdadm_conf.text;
79
80 packages = [ pkgs.mdadm ];
81 initrdBin = [ pkgs.mdadm ];
82 };
83
84 services.udev.packages = [ pkgs.mdadm ];
85 };
86 };
87}