nixos/zfs: expose zfs package as readonly attribute

This commit is contained in:
Jörg Thalheim 2021-01-15 13:13:51 +01:00
parent 1b718ca527
commit 648a6c4348
No known key found for this signature in database
GPG key ID: B3F5D81B0C6967C4

View file

@ -19,18 +19,8 @@ let
enableZfs = inInitrd || inSystem; enableZfs = inInitrd || inSystem;
kernel = config.boot.kernelPackages;
packages = if config.boot.zfs.enableUnstable then {
zfs = kernel.zfsUnstable;
zfsUser = pkgs.zfsUnstable;
} else {
zfs = kernel.zfs;
zfsUser = pkgs.zfs;
};
autosnapPkg = pkgs.zfstools.override { autosnapPkg = pkgs.zfstools.override {
zfs = packages.zfsUser; zfs = cfgZfs.package;
}; };
zfsAutoSnap = "${autosnapPkg}/bin/zfs-auto-snapshot"; zfsAutoSnap = "${autosnapPkg}/bin/zfs-auto-snapshot";
@ -111,6 +101,12 @@ in
options = { options = {
boot.zfs = { boot.zfs = {
package = mkOption {
readOnly = true;
type = types.package;
default = if config.boot.zfs.enableUnstable then pkgs.zfsUnstable else pkgs.zfs;
description = "Configured ZFS userland tools package.";
};
enableUnstable = mkOption { enableUnstable = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
@ -370,16 +366,22 @@ in
boot = { boot = {
kernelModules = [ "zfs" ]; kernelModules = [ "zfs" ];
extraModulePackages = with packages; [ zfs ];
extraModulePackages = [
(if config.boot.zfs.enableUnstable then
config.boot.kernelPackages.zfsUnstable
else
config.boot.kernelPackages.zfs)
];
}; };
boot.initrd = mkIf inInitrd { boot.initrd = mkIf inInitrd {
kernelModules = [ "zfs" ] ++ optional (!cfgZfs.enableUnstable) "spl"; kernelModules = [ "zfs" ] ++ optional (!cfgZfs.enableUnstable) "spl";
extraUtilsCommands = extraUtilsCommands =
'' ''
copy_bin_and_libs ${packages.zfsUser}/sbin/zfs copy_bin_and_libs ${cfgZfs.package}/sbin/zfs
copy_bin_and_libs ${packages.zfsUser}/sbin/zdb copy_bin_and_libs ${cfgZfs.package}/sbin/zdb
copy_bin_and_libs ${packages.zfsUser}/sbin/zpool copy_bin_and_libs ${cfgZfs.package}/sbin/zpool
''; '';
extraUtilsCommandsTest = mkIf inInitrd extraUtilsCommandsTest = mkIf inInitrd
'' ''
@ -433,7 +435,7 @@ in
services.zfs.zed.settings = { services.zfs.zed.settings = {
ZED_EMAIL_PROG = mkDefault "${pkgs.mailutils}/bin/mail"; ZED_EMAIL_PROG = mkDefault "${pkgs.mailutils}/bin/mail";
PATH = lib.makeBinPath [ PATH = lib.makeBinPath [
packages.zfsUser cfgZfs.package
pkgs.coreutils pkgs.coreutils
pkgs.curl pkgs.curl
pkgs.gawk pkgs.gawk
@ -461,18 +463,18 @@ in
"vdev_clear-led.sh" "vdev_clear-led.sh"
] ]
) )
(file: { source = "${packages.zfsUser}/etc/${file}"; }) (file: { source = "${cfgZfs.package}/etc/${file}"; })
// { // {
"zfs/zed.d/zed.rc".text = zedConf; "zfs/zed.d/zed.rc".text = zedConf;
"zfs/zpool.d".source = "${packages.zfsUser}/etc/zfs/zpool.d/"; "zfs/zpool.d".source = "${cfgZfs.package}/etc/zfs/zpool.d/";
}; };
system.fsPackages = [ packages.zfsUser ]; # XXX: needed? zfs doesn't have (need) a fsck system.fsPackages = [ cfgZfs.package ]; # XXX: needed? zfs doesn't have (need) a fsck
environment.systemPackages = [ packages.zfsUser ] environment.systemPackages = [ cfgZfs.package ]
++ optional cfgSnapshots.enable autosnapPkg; # so the user can run the command to see flags ++ optional cfgSnapshots.enable autosnapPkg; # so the user can run the command to see flags
services.udev.packages = [ packages.zfsUser ]; # to hook zvol naming, etc. services.udev.packages = [ cfgZfs.package ]; # to hook zvol naming, etc.
systemd.packages = [ packages.zfsUser ]; systemd.packages = [ cfgZfs.package ];
systemd.services = let systemd.services = let
getPoolFilesystems = pool: getPoolFilesystems = pool:
@ -506,8 +508,8 @@ in
environment.ZFS_FORCE = optionalString cfgZfs.forceImportAll "-f"; environment.ZFS_FORCE = optionalString cfgZfs.forceImportAll "-f";
script = (importLib { script = (importLib {
# See comments at importLib definition. # See comments at importLib definition.
zpoolCmd="${packages.zfsUser}/sbin/zpool"; zpoolCmd = "${cfgZfs.package}/sbin/zpool";
awkCmd="${pkgs.gawk}/bin/awk"; zfsCmd = "${cfgZfs.package}/sbin/zfs";
inherit cfgZfs; inherit cfgZfs;
}) + '' }) + ''
poolImported "${pool}" && exit poolImported "${pool}" && exit
@ -561,7 +563,7 @@ in
RemainAfterExit = true; RemainAfterExit = true;
}; };
script = '' script = ''
${packages.zfsUser}/sbin/zfs set nixos:shutdown-time="$(date)" "${pool}" ${cfgZfs.package}/sbin/zfs set nixos:shutdown-time="$(date)" "${pool}"
''; '';
}; };
createZfsService = serv: createZfsService = serv:
@ -633,11 +635,11 @@ in
Type = "oneshot"; Type = "oneshot";
}; };
script = '' script = ''
${packages.zfsUser}/bin/zpool scrub ${ ${cfgZfs.package}/bin/zpool scrub ${
if cfgScrub.pools != [] then if cfgScrub.pools != [] then
(concatStringsSep " " cfgScrub.pools) (concatStringsSep " " cfgScrub.pools)
else else
"$(${packages.zfsUser}/bin/zpool list -H -o name)" "$(${cfgZfs.package}/bin/zpool list -H -o name)"
} }
''; '';
}; };
@ -656,7 +658,7 @@ in
systemd.services.zpool-trim = { systemd.services.zpool-trim = {
description = "ZFS pools trim"; description = "ZFS pools trim";
after = [ "zfs-import.target" ]; after = [ "zfs-import.target" ];
path = [ packages.zfsUser ]; path = [ cfgZfs.package ];
startAt = cfgTrim.interval; startAt = cfgTrim.interval;
# By default we ignore errors returned by the trim command, in case: # By default we ignore errors returned by the trim command, in case:
# - HDDs are mixed with SSDs # - HDDs are mixed with SSDs