summaryrefslogtreecommitdiff
path: root/pkgs/tools/text/gnugrep/default.nix
blob: 9f0ecedd879289f9d6dc2e338a89094b6ef2c49e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
  lib,
  stdenv,
  updateAutotoolsGnuConfigScriptsHook,
  glibcLocales,
  fetchurl,
  pcre2,
  libiconv,
  perl,
  runtimeShellPackage,
}:

# Note: this package is used for bootstrapping fetchurl, and thus
# cannot use fetchpatch! All mutable patches (generated by GitHub or
# cgit) that are needed here should be included directly in Nixpkgs as
# files.

let
  version = "3.12";
in

stdenv.mkDerivation {
  pname = "gnugrep";
  inherit version;

  src = fetchurl {
    url = "mirror://gnu/grep/grep-${version}.tar.xz";
    hash = "sha256-JkmyfA6Q5jLq3NdXvgbG6aT0jZQd5R58D4P/dkCKB7k=";
  };

  patches = [
    # Fixes test-float-h failure on ppc64 with C23
    # https://lists.gnu.org/archive/html/bug-gnulib/2025-07/msg00021.html
    # Multiple upstream commits squashed with adjustments, see header
    ./gnulib-float-h-tests-port-to-C23-PowerPC-GCC.patch
  ];

  # Some gnulib tests fail
  # - on Musl: https://github.com/NixOS/nixpkgs/pull/228714
  # - on x86_64-darwin: https://github.com/NixOS/nixpkgs/pull/228714#issuecomment-1576826330
  # - when building on Darwin (cross-compilation): test-nl_langinfo-mt fails
  postPatch =
    if stdenv.hostPlatform.isMusl || stdenv.buildPlatform.isDarwin then
      ''
        sed -i 's:gnulib-tests::g' Makefile.in
      ''
    else
      null;

  nativeCheckInputs = [
    perl
    glibcLocales
  ];
  outputs = [
    "out"
    "info"
  ]; # the man pages are rather small

  nativeBuildInputs = [ updateAutotoolsGnuConfigScriptsHook ];
  buildInputs = [
    pcre2
    libiconv
  ]
  ++ lib.optional (!stdenv.hostPlatform.isWindows) runtimeShellPackage;

  # cygwin: FAIL: multibyte-white-space
  # freebsd: FAIL mb-non-UTF8-performance
  # x86_64-darwin: fails 'stack-overflow' tests on Rosetta 2 emulator
  # aarch32: fails 'stack-overflow' when run on qemu under x86_64
  doCheck =
    !stdenv.hostPlatform.isCygwin
    && !stdenv.hostPlatform.isFreeBSD
    && !(stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64)
    && !stdenv.buildPlatform.isRiscV64
    && !stdenv.hostPlatform.isAarch32;

  # On macOS, force use of mkdir -p, since Grep's fallback
  # (./install-sh) is broken.
  preConfigure = ''
    export MKDIR_P="mkdir -p"
  '';

  enableParallelBuilding = true;

  # Fix reference to sh in bootstrap-tools, and invoke grep via
  # absolute path rather than looking at argv[0].
  postInstall = ''
    rm $out/bin/egrep $out/bin/fgrep
    echo "#! /bin/sh" > $out/bin/egrep
    echo "exec $out/bin/grep -E \"\$@\"" >> $out/bin/egrep
    echo "#! /bin/sh" > $out/bin/fgrep
    echo "exec $out/bin/grep -F \"\$@\"" >> $out/bin/fgrep
    chmod +x $out/bin/egrep $out/bin/fgrep
  '';

  env = lib.optionalAttrs (stdenv.hostPlatform.isMinGW || stdenv.hostPlatform.isCygwin) {
    NIX_CFLAGS_COMPILE = "-Wno-error=format-security";
  };

  meta = {
    homepage = "https://www.gnu.org/software/grep/";
    description = "GNU implementation of the Unix grep command";

    longDescription = ''
      The grep command searches one or more input files for lines
      containing a match to a specified pattern.  By default, grep
      prints the matching lines.
    '';

    license = lib.licenses.gpl3Plus;

    maintainers = [
      lib.maintainers.das_j
      lib.maintainers.m00wl
    ];
    teams = [ lib.teams.security-review ];
    platforms = lib.platforms.all;
    mainProgram = "grep";
    identifiers.cpeParts = lib.meta.cpeFullVersionWithVendor "gnu" version // {
      product = "grep";
    };
  };

  passthru = {
    inherit pcre2;
  };
}