lol
1{ stdenv, fetchFromGitHub, autoreconfHook, utillinux, nukeReferences, coreutils, fetchpatch
2, configFile ? "all"
3
4# Userspace dependencies
5, zlib, libuuid, python, attr, openssl
6
7# Kernel dependencies
8, kernel ? null, spl ? null, splUnstable ? null, splLegacyCrypto ? null
9}:
10
11with stdenv.lib;
12let
13 buildKernel = any (n: n == configFile) [ "kernel" "all" ];
14 buildUser = any (n: n == configFile) [ "user" "all" ];
15
16 common = { version
17 , sha256
18 , extraPatches
19 , spl
20 , rev ? "zfs-${version}"
21 , isUnstable ? false
22 , isLegacyCrypto ? false
23 , incompatibleKernelVersion ? null } @ args:
24 if buildKernel &&
25 (incompatibleKernelVersion != null) &&
26 versionAtLeast kernel.version incompatibleKernelVersion then
27 throw ''
28 Linux v${kernel.version} is not yet supported by zfsonlinux v${version}.
29 ${stdenv.lib.optional (!isUnstable) "Try zfsUnstable or set the NixOS option boot.zfs.enableUnstable."}
30 ''
31 else stdenv.mkDerivation rec {
32 name = "zfs-${configFile}-${version}${optionalString buildKernel "-${kernel.version}"}";
33
34 src = fetchFromGitHub {
35 owner = "zfsonlinux";
36 repo = "zfs";
37 inherit rev sha256;
38 };
39
40 patches = extraPatches;
41
42 nativeBuildInputs = [ autoreconfHook nukeReferences ]
43 ++ optional buildKernel kernel.moduleBuildDependencies;
44 buildInputs =
45 optionals buildKernel [ spl ]
46 ++ optionals buildUser [ zlib libuuid python attr ]
47 ++ optionals (buildUser && (isUnstable || isLegacyCrypto)) [ openssl ];
48
49 # for zdb to get the rpath to libgcc_s, needed for pthread_cancel to work
50 NIX_CFLAGS_LINK = "-lgcc_s";
51
52 hardeningDisable = [ "pic" ];
53
54 preConfigure = ''
55 substituteInPlace ./module/zfs/zfs_ctldir.c --replace "umount -t zfs" "${utillinux}/bin/umount -t zfs"
56 substituteInPlace ./module/zfs/zfs_ctldir.c --replace "mount -t zfs" "${utillinux}/bin/mount -t zfs"
57 substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/umount" "${utillinux}/bin/umount"
58 substituteInPlace ./lib/libzfs/libzfs_mount.c --replace "/bin/mount" "${utillinux}/bin/mount"
59 substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/ztest" "$out/sbin/ztest"
60 substituteInPlace ./cmd/ztest/ztest.c --replace "/usr/sbin/zdb" "$out/sbin/zdb"
61 substituteInPlace ./config/user-systemd.m4 --replace "/usr/lib/modules-load.d" "$out/etc/modules-load.d"
62 substituteInPlace ./config/zfs-build.m4 --replace "\$sysconfdir/init.d" "$out/etc/init.d"
63 substituteInPlace ./etc/zfs/Makefile.am --replace "\$(sysconfdir)" "$out/etc"
64 substituteInPlace ./cmd/zed/Makefile.am --replace "\$(sysconfdir)" "$out/etc"
65 substituteInPlace ./module/Makefile.in --replace "/bin/cp" "cp"
66 substituteInPlace ./etc/systemd/system/zfs-share.service.in \
67 --replace "@bindir@/rm " "${coreutils}/bin/rm "
68
69 for f in ./udev/rules.d/*
70 do
71 substituteInPlace "$f" --replace "/lib/udev/vdev_id" "$out/lib/udev/vdev_id"
72 done
73
74 ./autogen.sh
75 '';
76
77 configureFlags = [
78 "--with-config=${configFile}"
79 ] ++ optionals buildUser [
80 "--with-dracutdir=$(out)/lib/dracut"
81 "--with-udevdir=$(out)/lib/udev"
82 "--with-systemdunitdir=$(out)/etc/systemd/system"
83 "--with-systemdpresetdir=$(out)/etc/systemd/system-preset"
84 "--with-mounthelperdir=$(out)/bin"
85 "--sysconfdir=/etc"
86 "--localstatedir=/var"
87 "--enable-systemd"
88 ] ++ optionals buildKernel [
89 "--with-spl=${spl}/libexec/spl"
90 "--with-linux=${kernel.dev}/lib/modules/${kernel.modDirVersion}/source"
91 "--with-linux-obj=${kernel.dev}/lib/modules/${kernel.modDirVersion}/build"
92 ];
93
94 enableParallelBuilding = true;
95
96 installFlags = [
97 "sysconfdir=\${out}/etc"
98 "DEFAULT_INITCONF_DIR=\${out}/default"
99 ];
100
101 postInstall = ''
102 # Prevent kernel modules from depending on the Linux -dev output.
103 nuke-refs $(find $out -name "*.ko")
104 '' + optionalString buildUser ''
105 # Remove provided services as they are buggy
106 rm $out/etc/systemd/system/zfs-import-*.service
107
108 sed -i '/zfs-import-scan.service/d' $out/etc/systemd/system/*
109
110 for i in $out/etc/systemd/system/*; do
111 substituteInPlace $i --replace "zfs-import-cache.service" "zfs-import.target"
112 done
113
114 # Fix pkgconfig.
115 ln -s ../share/pkgconfig $out/lib/pkgconfig
116
117 # Remove tests because they add a runtime dependency on gcc
118 rm -rf $out/share/zfs/zfs-tests
119 '';
120
121 outputs = [ "out" ] ++ optionals buildUser [ "lib" "dev" ];
122
123 meta = {
124 description = "ZFS Filesystem Linux Kernel module";
125 longDescription = ''
126 ZFS is a filesystem that combines a logical volume manager with a
127 Copy-On-Write filesystem with data integrity detection and repair,
128 snapshotting, cloning, block devices, deduplication, and more.
129 '';
130 homepage = http://zfsonlinux.org/;
131 license = licenses.cddl;
132 platforms = platforms.linux;
133 maintainers = with maintainers; [ jcumming wizeman wkennington fpletz globin ];
134 };
135 };
136in {
137 # also check if kernel version constraints in
138 # ./nixos/modules/tasks/filesystems/zfs.nix needs
139 # to be adapted
140 zfsStable = common {
141 # comment/uncomment if breaking kernel versions are known
142 incompatibleKernelVersion = null;
143
144 # this package should point to the latest release.
145 version = "0.7.6";
146
147 sha256 = "1k3a69zfdk4ia4z2l69lbz0mj26bwdanxd2wynkdpm2kl3zjj18h";
148
149 extraPatches = [
150 (fetchpatch {
151 url = "https://github.com/Mic92/zfs/compare/zfs-0.7.0-rc3...nixos-zfs-0.7.0-rc3.patch";
152 sha256 = "1vlw98v8xvi8qapzl1jwm69qmfslwnbg3ry1lmacndaxnyckkvhh";
153 })
154 ];
155
156 inherit spl;
157 };
158
159 zfsUnstable = common {
160 # comment/uncomment if breaking kernel versions are known
161 incompatibleKernelVersion = null;
162
163 # this package should point to a version / git revision compatible with the latest kernel release
164 version = "2018-02-02";
165
166 rev = "fbd42542686af053f0d162ec4630ffd4fff1cc30";
167 sha256 = "0qzkwnnk7kz1hwvcaqlpzi5yspfhhmd2alklc07k056ddzbx52qb";
168 isUnstable = true;
169
170 extraPatches = [
171 (fetchpatch {
172 url = "https://github.com/Mic92/zfs/compare/fbd42542686af053f0d162ec4630ffd4fff1cc30...nixos-zfs-2018-02-02.patch";
173 sha256 = "05wqwjm9648x60vkwxbp8l6z1q73r2a5l2ni28i2f4pla8s3ahln";
174 })
175 ];
176
177 spl = splUnstable;
178 };
179
180 zfsLegacyCrypto = common {
181 # comment/uncomment if breaking kernel versions are known
182 incompatibleKernelVersion = null;
183
184 # this package should point to a version / git revision compatible with the latest kernel release
185 version = "2018-02-01";
186
187 rev = "4c46b99d24a6e71b3c72462c11cb051d0930ad60";
188 sha256 = "011lcp2x44jgfzqqk2gjmyii1v7rxcprggv20prxa3c552drsx3c";
189 isUnstable = true;
190
191 extraPatches = [
192 (fetchpatch {
193 url = "https://github.com/Mic92/zfs/compare/4c46b99d24a6e71b3c72462c11cb051d0930ad60...nixos-zfs-2018-02-01.patch";
194 sha256 = "1gqmgqi39qhk5kbbvidh8f2xqq25vj58i9x0wjqvcx6a71qj49ch";
195 })
196 ];
197
198 spl = splLegacyCrypto;
199 };
200
201}