From 536f2e6fdee199f09c50232e70f815e2b42e169f Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Fri, 14 Nov 2025 16:44:27 +0000 Subject: [PATCH 1/2] nix: Initial commit of flake --- flake.lock | 61 +++++++++++++++++++++ flake.nix | 32 +++++++++++ nix/default.nix | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 231 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/default.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000000..6ed149ddb7d5 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1762977756, + "narHash": "sha256-4PqRErxfe+2toFJFgcRKZ0UI9NSIOJa+7RXVtBhy4KE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c5ae371f1a6a7fd27823bc500d9390b38c05fa55", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000000..a7afe9cabb2b --- /dev/null +++ b/flake.nix @@ -0,0 +1,32 @@ +{ + description = "Nix flake for OpenZFS (ZFS)"; + + # The inputs, such as Nixpkgs and flake-utils + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; # Adjust to the desired channel or version + + flake-utils.url = "github:numtide/flake-utils"; + }; + + # Outputs from this flake (e.g. packages, system configurations) + outputs = + { + nixpkgs, + flake-utils, + ... + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + }; + openzfs = pkgs.callPackage ./nix/default.nix { + kernel = pkgs.linux_6_12; + }; + in + { + packages.default = openzfs; + } + ); +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 000000000000..a74f2b3119c3 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,138 @@ +{ + stdenv, + lib, + pkgs, + kernel, + kernelModuleMakeFlags ? [ ], + autoreconfHook, + attr, + coreutils, + gawk, + gnused, + gnugrep, + ksh, + libtirpc, + libuuid, + nukeReferences, + openssl, + pam, + pkg-config, + python3, + systemd, + udevCheckHook, + zlib, +}: + +stdenv.mkDerivation { + pname = "zfs"; + version = "2.3.4-${kernel.version}"; # Specify the version of ZFS you want + + src = ./..; + + preConfigure = '' + # The kernel module builds some tests during the configurePhase, this envvar controls their parallelism + export TEST_JOBS=$NIX_BUILD_CORES + if [ -z "$enableParallelBuilding" ]; then + export TEST_JOBS=1 + fi + ''; + + postPatch = '' + patchShebangs scripts tests + + substituteInPlace ./config/user-systemd.m4 --replace-fail "/usr/lib/modules-load.d" "$out/etc/modules-load.d" + substituteInPlace ./config/zfs-build.m4 --replace-fail "\$sysconfdir/init.d" "$out/etc/init.d" \ + --replace-fail "/etc/default" "$out/etc/default" + substituteInPlace ./contrib/initramfs/Makefile.am \ + --replace-fail "/usr/share/initramfs-tools" "$out/usr/share/initramfs-tools" + + substituteInPlace ./udev/vdev_id \ + --replace-fail "PATH=/bin:/sbin:/usr/bin:/usr/sbin" \ + "PATH=${ + lib.makeBinPath [ + coreutils + gawk + gnused + gnugrep + systemd + ] + }" + + substituteInPlace ./config/zfs-build.m4 \ + --replace-fail "bashcompletiondir=/etc/bash_completion.d" \ + "bashcompletiondir=$out/share/bash-completion/completions" \ + --replace-fail 'DEBUG_CFLAGS="-Werror"' ' ' + + # Tests + # Not required with Nix + sed -i s/"^constrain_path$"/""/ scripts/zfs-tests.sh + substituteInPlace ./scripts/zfs-tests.sh \ + --replace-fail '"$STF_PATH/ksh"' "${lib.getExe pkgs.ksh}" + substituteInPlace ./tests/test-runner/bin/test-runner.py.in \ + --replace-fail "/usr/share/zfs/" "$out/share/zfs/" \ + --replace-fail "KILL = 'kill'" "KILL = '${lib.getExe' pkgs.coreutils "kill"}'" \ + --replace-fail "SUDO = 'sudo'" "SUDO = '/run/wrappers/bin/sudo'" \ + --replace-fail "TRUE = 'true'" "TRUE = '${lib.getExe' pkgs.coreutils "true"}'" + ''; + + buildInputs = [ + attr + libtirpc + libuuid + openssl + pam + python3 + systemd + zlib + ]; + + nativeBuildInputs = [ + autoreconfHook + ksh + nukeReferences + pkg-config + udevCheckHook + kernel.moduleBuildDependencies + ]; + + enableParallelBuilding = true; + + configureFlags = [ + "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source" + "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build" + "--with-dracutdir=$(out)/lib/dracut" + "--with-udevdir=$(out)/lib/udev" + "--with-systemdunitdir=$(out)/etc/systemd/system" + "--with-systemdpresetdir=$(out)/etc/systemd/system-preset" + "--with-systemdgeneratordir=$(out)/lib/systemd/system-generator" + "--with-mounthelperdir=$(out)/bin" + "--libexecdir=$(out)/libexec" + "--sysconfdir=/etc" + "--localstatedir=/var" + "--enable-systemd" + "--enable-pam" + + # Debug + "--enable-debug" + "--enable-debuginfo" + "--enable-asan" + "--enable-ubsan" + "--enable-debug-kmem" + "--enable-debug-kmem-tracking" + ] + ++ map (f: "KERNEL_${f}") kernelModuleMakeFlags; + + doInstallCheck = true; + + installFlags = [ + "sysconfdir=\${out}/etc" + "DEFAULT_INITCONF_DIR=\${out}/default" + "INSTALL_MOD_PATH=\${out}" + ]; + + meta = with lib; { + description = "OpenZFS on Linux"; + license = licenses.cddl; + platforms = platforms.linux; + }; +} From 4dcf2fa6e8cba44ea825c98c7f1efb86639c0c83 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Sat, 15 Nov 2025 12:27:10 +0000 Subject: [PATCH 2/2] nix: Add NixOS integration test definition for ZTS --- flake.nix | 2 ++ nix/test.nix | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 nix/test.nix diff --git a/flake.nix b/flake.nix index a7afe9cabb2b..ec86e9f61aa5 100644 --- a/flake.nix +++ b/flake.nix @@ -27,6 +27,8 @@ in { packages.default = openzfs; + + checks.zts = pkgs.testers.runNixOSTest (import ./nix/test.nix { inherit openzfs; }); } ); } diff --git a/nix/test.nix b/nix/test.nix new file mode 100644 index 000000000000..a52897d8c3aa --- /dev/null +++ b/nix/test.nix @@ -0,0 +1,95 @@ +{ openzfs, ... }: + +{ + name = "zts"; + + globalTimeout = 24 * 60 * 60; + + nodes = { + machine = + { pkgs, lib, ... }: + { + virtualisation = { + cores = 4; + diskSize = 10 * 1024; + memorySize = 16 * 1024; + }; + + boot.kernelPatches = [ + { + name = "enable KASAN"; + patch = null; + extraConfig = '' + KASAN y + DEBUG_KMEMLEAK y + GCOV_KERNEL y + ''; + } + ]; + + boot.kernelParams = [ + "kasan.fault=report" + ]; + + boot.loader.systemd-boot.enable = true; + boot.initrd.systemd.enable = true; + + networking.hostId = "deadbeef"; + boot.zfs.package = openzfs; + boot.zfs.modulePackage = openzfs; + boot.supportedFilesystems = [ "zfs" ]; + + environment.systemPackages = + let + zfsTestsScript = pkgs.writeShellScriptBin "zfs-tests" '' + export STF_PATH=${ + lib.makeBinPath [ + pkgs.bash + pkgs.coreutils + pkgs.bzip2 + pkgs.gawk + pkgs.ksh + pkgs.lvm2 + pkgs.su + pkgs.sudo + pkgs.systemd + pkgs.util-linux + openzfs + ] + }; + + export LOSETUP="${lib.getExe' pkgs.util-linux "losetup"}" + export DMSETUP="${lib.getExe' pkgs.lvm2.bin "dmsetup"}" + + ${openzfs}/share/zfs/zfs-tests.sh -K -m -v -x + ''; + in + [ + zfsTestsScript + openzfs + ]; + + users.users.tester = { + isNormalUser = true; + description = "ZFS tester"; + password = "foobar"; + extraGroups = [ "wheel" ]; + }; + + security.sudo.wheelNeedsPassword = false; + }; + + }; + + testScript = '' + machine.wait_for_unit("multi-user.target") + + machine.log(machine.succeed( + "sudo -u tester -- zfs-tests" + )) + + _, report_location = machine.execute("find /tmp -name zts-report") + + machine.copy_from_vm(report_location, "") + ''; +}