2022-04-27 09:35:20 +00:00
|
|
|
{ config, pkgs, lib, ... }: let
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
cfg = config.boot.swraid;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
mdadm_conf = config.environment.etc."mdadm.conf";
|
|
|
|
|
|
|
|
enable_implicitly_for_old_state_versions = lib.versionOlder config.system.stateVersion "23.11";
|
|
|
|
|
|
|
|
minimum_config_is_set = config_text:
|
|
|
|
(builtins.match ".*(MAILADDR|PROGRAM).*" mdadm_conf.text) != null;
|
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
in {
|
2023-07-15 17:15:38 +00:00
|
|
|
imports = [
|
|
|
|
(lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "enable" ] [ "boot" "swraid" "enable" ])
|
|
|
|
(lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "mdadmConf" ] [ "boot" "swraid" "mdadmConf" ])
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
'';
|
2023-10-09 19:29:22 +00:00
|
|
|
default = enable_implicitly_for_old_state_versions;
|
2023-07-15 17:15:38 +00:00
|
|
|
defaultText = lib.mdDoc "`true` if stateVersion is older than 23.11";
|
2022-04-27 09:35:20 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
mdadmConf = lib.mkOption {
|
2023-07-15 17:15:38 +00:00
|
|
|
description = lib.mdDoc "Contents of {file}`/etc/mdadm.conf`.";
|
2022-04-27 09:35:20 +00:00
|
|
|
type = lib.types.lines;
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2023-10-09 19:29:22 +00:00
|
|
|
warnings = lib.mkIf
|
|
|
|
( !enable_implicitly_for_old_state_versions && !minimum_config_is_set mdadm_conf)
|
|
|
|
[ "mdadm: Neither MAILADDR nor PROGRAM has been set. This will cause the `mdmon` service to crash." ];
|
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
environment.systemPackages = [ pkgs.mdadm ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
environment.etc."mdadm.conf".text = lib.mkAfter cfg.mdadmConf;
|
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
services.udev.packages = [ pkgs.mdadm ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
systemd.packages = [ pkgs.mdadm ];
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
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/
|
|
|
|
'';
|
2022-04-27 09:35:20 +00:00
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
extraUtilsCommands = lib.mkIf (!config.boot.initrd.systemd.enable) ''
|
2023-07-15 17:15:38 +00:00
|
|
|
# Add RAID mdadm tool.
|
|
|
|
copy_bin_and_libs ${pkgs.mdadm}/sbin/mdadm
|
|
|
|
copy_bin_and_libs ${pkgs.mdadm}/sbin/mdmon
|
|
|
|
'';
|
2022-04-27 09:35:20 +00:00
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
extraUtilsCommandsTest = lib.mkIf (!config.boot.initrd.systemd.enable) ''
|
2023-07-15 17:15:38 +00:00
|
|
|
$out/bin/mdadm --version
|
|
|
|
'';
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
extraFiles."/etc/mdadm.conf".source = pkgs.writeText "mdadm.conf" mdadm_conf.text;
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
systemd = {
|
2023-10-09 19:29:22 +00:00
|
|
|
contents."/etc/mdadm.conf".text = mdadm_conf.text;
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
packages = [ pkgs.mdadm ];
|
|
|
|
initrdBin = [ pkgs.mdadm ];
|
2022-04-27 09:35:20 +00:00
|
|
|
};
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
services.udev.packages = [ pkgs.mdadm ];
|
2022-04-27 09:35:20 +00:00
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|