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