1{ stdenv, fetchFromGitHub, autoreconfHook, utillinux, nukeReferences, coreutils
2, configFile ? "all"
3
4# Userspace dependencies
5, zlib, libuuid, python
6
7# Kernel dependencies
8, kernel ? null, spl ? null
9}:
10
11with stdenv.lib;
12let
13 buildKernel = any (n: n == configFile) [ "kernel" "all" ];
14 buildUser = any (n: n == configFile) [ "user" "all" ];
15in
16
17assert any (n: n == configFile) [ "kernel" "user" "all" ];
18assert buildKernel -> kernel != null && spl != null;
19
20stdenv.mkDerivation rec {
21 name = "zfs-${configFile}-${version}${optionalString buildKernel "-${kernel.version}"}";
22
23 version = "0.6.5.2";
24
25 src = fetchFromGitHub {
26 owner = "zfsonlinux";
27 repo = "zfs";
28 rev = "zfs-${version}";
29 sha256 = "0246cypa65rjm8j2123al4x4cwpydqwqrg828pxcpk08v1djy3v1";
30 };
31
32 patches = [ ./nix-build.patch ];
33
34 buildInputs = [ autoreconfHook nukeReferences ]
35 ++ optionals buildKernel [ spl ]
36 ++ optionals buildUser [ zlib libuuid python ];
37
38 # for zdb to get the rpath to libgcc_s, needed for pthread_cancel to work
39 NIX_CFLAGS_LINK = "-lgcc_s";
40
41 preConfigure = ''
42 substituteInPlace ./module/zfs/zfs_ctldir.c --replace "umount -t zfs" "${utillinux}/bin/umount -t zfs"
43 substituteInPlace ./module/zfs/zfs_ctldir.c --replace "mount -t zfs" "${utillinux}/bin/mount -t zfs"
44 substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/umount" "${utillinux}/bin/umount"
45 substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/mount" "${utillinux}/bin/mount"
46 substituteInPlace ./udev/rules.d/* --replace "/lib/udev/vdev_id" "$out/lib/udev/vdev_id"
47 substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/ztest" "$out/sbin/ztest"
48 substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/zdb" "$out/sbin/zdb"
49 substituteInPlace ./config/user-systemd.m4 --replace "/usr/lib/modules-load.d" "$out/etc/modules-load.d"
50 substituteInPlace ./config/zfs-build.m4 --replace "\$sysconfdir/init.d" "$out/etc/init.d"
51 substituteInPlace ./etc/zfs/Makefile.am --replace "\$(sysconfdir)" "$out/etc"
52 substituteInPlace ./cmd/zed/Makefile.am --replace "\$(sysconfdir)" "$out/etc"
53 substituteInPlace ./module/Makefile.in --replace "/bin/cp" "cp"
54 substituteInPlace ./etc/systemd/system/zfs-share.service.in \
55 --replace "@bindir@/rm " "${coreutils}/bin/rm "
56 ./autogen.sh
57 '';
58
59 configureFlags = [
60 "--with-config=${configFile}"
61 ] ++ optionals buildUser [
62 "--with-dracutdir=$(out)/lib/dracut"
63 "--with-udevdir=$(out)/lib/udev"
64 "--with-systemdunitdir=$(out)/etc/systemd/system"
65 "--with-systemdpresetdir=$(out)/etc/systemd/system-preset"
66 "--with-mounthelperdir=$(out)/bin"
67 "--sysconfdir=/etc"
68 "--localstatedir=/var"
69 "--enable-systemd"
70 ] ++ optionals buildKernel [
71 "--with-spl=${spl}/libexec/spl"
72 "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source"
73 "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
74 ];
75
76 enableParallelBuilding = true;
77
78 installFlags = [
79 "sysconfdir=\${out}/etc"
80 "DEFAULT_INITCONF_DIR=\${out}/default"
81 ];
82
83 postInstall = ''
84 # Prevent kernel modules from depending on the Linux -dev output.
85 nuke-refs $(find $out -name "*.ko")
86 '' + optionalString buildUser ''
87 # Remove provided services as they are buggy
88 rm $out/etc/systemd/system/zfs-import-*.service
89
90 sed -i '/zfs-import-scan.service/d' $out/etc/systemd/system/*
91
92 for i in $out/etc/systemd/system/*; do
93 substituteInPlace $i --replace "zfs-import-cache.service" "zfs-import.target"
94 done
95
96 # Fix pkgconfig.
97 ln -s ../share/pkgconfig $out/lib/pkgconfig
98 '';
99
100 meta = {
101 description = "ZFS Filesystem Linux Kernel module";
102 longDescription = ''
103 ZFS is a filesystem that combines a logical volume manager with a
104 Copy-On-Write filesystem with data integrity detection and repair,
105 snapshotting, cloning, block devices, deduplication, and more.
106 '';
107 homepage = http://zfsonlinux.org/;
108 license = licenses.cddl;
109 platforms = platforms.linux;
110 maintainers = with maintainers; [ jcumming wizeman wkennington ];
111 };
112}