1{ lib
2, stdenv
3, fetchurl
4, autoreconfHook
5, buildPackages
6, libiconv
7, perl
8, texinfo
9, xz
10, gmpSupport ? true, gmp
11, aclSupport ? stdenv.isLinux, acl
12, attrSupport ? stdenv.isLinux, attr
13, selinuxSupport ? false, libselinux, libsepol
14# No openssl in default version, so openssl-induced rebuilds aren't too big.
15# It makes *sum functions significantly faster.
16, minimal ? true
17, withOpenssl ? !minimal, openssl
18, withPrefix ? false
19, singleBinary ? "symlinks" # you can also pass "shebangs" or false
20}:
21
22# Note: this package is used for bootstrapping fetchurl, and thus cannot use
23# fetchpatch! All mutable patches (generated by GitHub or cgit) that are needed
24# here should be included directly in Nixpkgs as files.
25
26assert aclSupport -> acl != null;
27assert selinuxSupport -> libselinux != null && libsepol != null;
28
29let
30 inherit (lib) concatStringsSep isString optional optionals optionalString;
31 isCross = (stdenv.hostPlatform != stdenv.buildPlatform);
32in
33stdenv.mkDerivation rec {
34 pname = "coreutils" + (optionalString (!minimal) "-full");
35 version = "9.3";
36
37 src = fetchurl {
38 url = "mirror://gnu/coreutils/coreutils-${version}.tar.xz";
39 hash = "sha256-rbz8/omSNbceh2jc8HzVMlILf1T5qAZIQ/jRmakEu6o=";
40 };
41
42 postPatch = ''
43 # The test tends to fail on btrfs, f2fs and maybe other unusual filesystems.
44 sed '2i echo Skipping dd sparse test && exit 77' -i ./tests/dd/sparse.sh
45 sed '2i echo Skipping du threshold test && exit 77' -i ./tests/du/threshold.sh
46 sed '2i echo Skipping cp reflink-auto test && exit 77' -i ./tests/cp/reflink-auto.sh
47 sed '2i echo Skipping cp sparse test && exit 77' -i ./tests/cp/sparse.sh
48 sed '2i echo Skipping rm deep-2 test && exit 77' -i ./tests/rm/deep-2.sh
49 sed '2i echo Skipping du long-from-unreadable test && exit 77' -i ./tests/du/long-from-unreadable.sh
50
51 # Some target platforms, especially when building inside a container have
52 # issues with the inotify test.
53 sed '2i echo Skipping tail inotify dir recreate test && exit 77' -i ./tests/tail-2/inotify-dir-recreate.sh
54
55 # sandbox does not allow setgid
56 sed '2i echo Skipping chmod setgid test && exit 77' -i ./tests/chmod/setgid.sh
57 substituteInPlace ./tests/install/install-C.sh \
58 --replace 'mode3=2755' 'mode3=1755'
59
60 # Fails on systems with a rootfs. Looks like a bug in the test, see
61 # https://lists.gnu.org/archive/html/bug-coreutils/2019-12/msg00000.html
62 sed '2i print "Skipping df skip-rootfs test"; exit 77' -i ./tests/df/skip-rootfs.sh
63
64 # these tests fail in the unprivileged nix sandbox (without nix-daemon) as we break posix assumptions
65 for f in ./tests/chgrp/{basic.sh,recurse.sh,default-no-deref.sh,no-x.sh,posix-H.sh}; do
66 sed '2i echo Skipping chgrp && exit 77' -i "$f"
67 done
68 for f in gnulib-tests/{test-chown.c,test-fchownat.c,test-lchown.c}; do
69 echo "int main() { return 77; }" > "$f"
70 done
71
72 # intermittent failures on builders, unknown reason
73 sed '2i echo Skipping du basic test && exit 77' -i ./tests/du/basic.sh
74 '' + (optionalString (stdenv.hostPlatform.libc == "musl") (concatStringsSep "\n" [
75 ''
76 echo "int main() { return 77; }" > gnulib-tests/test-parse-datetime.c
77 echo "int main() { return 77; }" > gnulib-tests/test-getlogin.c
78 ''
79 ])) + (optionalString stdenv.isAarch64 ''
80 sed '2i print "Skipping tail assert test"; exit 77' -i ./tests/tail-2/assert.sh
81
82 # Sometimes fails: https://github.com/NixOS/nixpkgs/pull/143097#issuecomment-954462584
83 sed '2i echo Skipping cut huge range test && exit 77' -i ./tests/misc/cut-huge-range.sh
84 '');
85
86 outputs = [ "out" "info" ];
87 separateDebugInfo = true;
88
89 nativeBuildInputs = [
90 # autoreconfHook is due to patch, normally only needed for cygwin
91 autoreconfHook
92 perl
93 xz.bin
94 ]
95 ++ optionals stdenv.hostPlatform.isCygwin [
96 # due to patch
97 texinfo
98 ];
99
100 buildInputs = [ ]
101 ++ optional aclSupport acl
102 ++ optional attrSupport attr
103 ++ optional gmpSupport gmp
104 ++ optional withOpenssl openssl
105 ++ optionals selinuxSupport [ libselinux libsepol ]
106 # TODO(@Ericson2314): Investigate whether Darwin could benefit too
107 ++ optional (isCross && stdenv.hostPlatform.libc != "glibc") libiconv;
108
109 configureFlags = [ "--with-packager=https://nixos.org" ]
110 ++ optional (singleBinary != false)
111 ("--enable-single-binary" + optionalString (isString singleBinary) "=${singleBinary}")
112 ++ optional withOpenssl "--with-openssl"
113 ++ optional stdenv.hostPlatform.isSunOS "ac_cv_func_inotify_init=no"
114 ++ optional withPrefix "--program-prefix=g"
115 # the shipped configure script doesn't enable nls, but using autoreconfHook
116 # does so which breaks the build
117 ++ optional stdenv.isDarwin "--disable-nls"
118 ++ optionals (isCross && stdenv.hostPlatform.libc == "glibc") [
119 # TODO(19b98110126fde7cbb1127af7e3fe1568eacad3d): Needed for fstatfs() I
120 # don't know why it is not properly detected cross building with glibc.
121 "fu_cv_sys_stat_statfs2_bsize=yes"
122 ]
123 # /proc/uptime is available on Linux and produces accurate results even if
124 # the boot time is set to the epoch because the system has no RTC. We
125 # explicitly enable it for cases where it can't be detected automatically,
126 # such as when cross-compiling.
127 ++ optional stdenv.hostPlatform.isLinux "gl_cv_have_proc_uptime=yes";
128
129 # The tests are known broken on Cygwin
130 # (http://article.gmane.org/gmane.comp.gnu.core-utils.bugs/19025),
131 # Darwin (http://article.gmane.org/gmane.comp.gnu.core-utils.bugs/19351),
132 # and {Open,Free}BSD.
133 # With non-standard storeDir: https://github.com/NixOS/nix/issues/512
134 # On aarch64+musl, test-init.sh fails due to a segfault in diff.
135 doCheck = (!isCross)
136 && (stdenv.hostPlatform.libc == "glibc" || stdenv.hostPlatform.libc == "musl")
137 && !(stdenv.hostPlatform.libc == "musl" && stdenv.hostPlatform.isAarch64)
138 && !stdenv.isAarch32;
139
140 # Prevents attempts of running 'help2man' on cross-built binaries.
141 PERL = if isCross then "missing" else null;
142
143 enableParallelBuilding = true;
144
145 NIX_LDFLAGS = optionalString selinuxSupport "-lsepol";
146 FORCE_UNSAFE_CONFIGURE = optionalString stdenv.hostPlatform.isSunOS "1";
147 env.NIX_CFLAGS_COMPILE = toString ([]
148 # Work around a bogus warning in conjunction with musl.
149 ++ optional stdenv.hostPlatform.isMusl "-Wno-error"
150 ++ optional stdenv.hostPlatform.isAndroid "-D__USE_FORTIFY_LEVEL=0");
151
152 # Works around a bug with 8.26:
153 # Makefile:3440: *** Recursive variable 'INSTALL' references itself (eventually). Stop.
154 preInstall = optionalString isCross ''
155 sed -i Makefile -e 's|^INSTALL =.*|INSTALL = ${buildPackages.coreutils}/bin/install -c|'
156 '';
157
158 postInstall = optionalString (isCross && !minimal) ''
159 rm $out/share/man/man1/*
160 cp ${buildPackages.coreutils-full}/share/man/man1/* $out/share/man/man1
161 ''
162 # du: 8.7 M locale + 0.4 M man pages
163 + optionalString minimal ''
164 rm -r "$out/share"
165 '';
166
167 meta = with lib; {
168 homepage = "https://www.gnu.org/software/coreutils/";
169 description = "The GNU Core Utilities";
170 longDescription = ''
171 The GNU Core Utilities are the basic file, shell and text manipulation
172 utilities of the GNU operating system. These are the core utilities which
173 are expected to exist on every operating system.
174 '';
175 license = licenses.gpl3Plus;
176 maintainers = with maintainers; [ das_j ];
177 platforms = with platforms; unix ++ windows;
178 priority = 10;
179 };
180}