NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.15k stars 14.18k forks source link

Setup script fails with '$'\r': command not found' error, preventing package builds #339726

Closed Bitbitti closed 2 months ago

Bitbitti commented 2 months ago

I'm encountering a persistent error when trying to build packages on NixOS. The error occurs in the Nix setup script, specifically on line 1579: /nix/store/bc0as359y5y067dfp7afrq1f011m9ny3-stdenv-linux/setup: line 1579: $'\r': command not found This error is preventing me from building packages, including a custom Canon printer driver package.

System Information:

Steps to Reproduce:

  1. Create a custom package derivation (in my case, a Canon printer driver)
  2. Attempt to build the package using nixos-rebuild switch
  3. The build fails with the above error

Full Error Output:

building '/nix/store/q0qfr9f72n8gf26wcnd87wq5i2kn502c-canon-cups-ufr2-custom-5.70.drv'... Running phase: unpackPhase /nix/store/bc0as359y5y067dfp7afrq1f011m9ny3-stdenv-linux/setup: line 1579: $'\r': command not found error: builder for '/nix/store/q0qfr9f72n8gf26wcnd87wq5i2kn502c-canon-cups-ufr2-custom-5.70.drv' failed with exit code 127 error: 1 dependencies of derivation '/nix/store/j95fimzqbq8qja80h6fn2vlmg6979807-X-Restart-Triggers-systemd-udevd.drv' failed to build error: 1 dependencies of derivation '/nix/store/8la17r5zbv3q4w09plqswh8x7ynqdl8v-cups-progs.drv' failed to build error: 1 dependencies of derivation '/nix/store/jjwj0b0k1i2d22lr28lr3hbj8l36y3r2-hwdb.bin.drv' failed to build error: 1 dependencies of derivation '/nix/store/fwrn6w0915rhm6jwg6lqqih7fd7223ca-unit-script-add-canon-printer-start.drv' failed to build error: 1 dependencies of derivation '/nix/store/f5ria032hpr3h40mmlxk0xv20krzrv0i-etc.drv' failed to build error: 1 dependencies of derivation '/nix/store/lml2y7gpy1qdw9klcryyylig4xxxam75-nixos-system-nixos-24.05.4169.797f7dc49e0b.drv' failed to build

Things I've Tried:

  1. Updating Nix and NixOS to the latest version
  2. Modifying the package derivation to use dos2unix on all files
  3. Using gnutar instead of the default tar for extraction
  4. Running nix-store --verify --check-contents and nix-store --repair
  5. Adjusting the use of find, xargs, and dos2unix in the derivation

None of these attempts have resolved the issue. The error persists in the Nix setup script itself, suggesting it might be a core Nix issue rather than a problem with my specific derivation.

Additional Information:

Questions:

  1. Is this a known issue with the Nix setup script?
  2. Are there any workarounds or fixes that can be applied at the user level?
  3. Could this be related to how Nix is handling line endings in its core scripts?

Any assistance or insight into this issue would be greatly appreciated. Thank you for your time and effort in addressing this problem.

Additional Information:

The error occurs specifically on line 1579, which is: eval "${!curPhase:-$curPhase}"

ghpzin commented 2 months ago

Could you include custom package you are trying to build.

Bitbitti commented 2 months ago

Thank you for your quick response. Here is the custom package derivation I'm trying to build, along with the preparation script:

  1. The derivation file (default.nix):

{ stdenv
, lib
, fetchurl
, dpkg
, makeWrapper
, autoPatchelfHook
, cups
, libusb1
, libtool
, glib
, glibc
, gnused
, gnutar
, gzip
, dos2unix
}:

stdenv.mkDerivation rec {
  pname = "canon-cups-ufr2-custom";
  version = "5.70";

  src = fetchurl {
    url = "https://gdlp01.c-wss.com/gds/4/0100010264/01/linux-UFRII-drv-v370-uken-07.tar.gz";
    sha256 = "01nxpg3h1c64p5skxv904fg5c4sblmif486vkij2v62wwn6l65pz";
  };

  nativeBuildInputs = [ dpkg makeWrapper autoPatchelfHook gnutar gzip gnused dos2unix ];
  buildInputs = [ cups libusb1 libtool glib glibc ];

  unpackPhase = ''
    runHook preUnpack
    ${gnutar}/bin/tar xzf $src
    find . -type f -exec ${dos2unix}/bin/dos2unix {} +
    cd linux-UFRII-drv-v370-uken
    ${gnutar}/bin/tar xzf Sources/cndrvcups-lb-3.70-1.tar.gz
    find . -type f -exec ${dos2unix}/bin/dos2unix {} +
    runHook postUnpack
  '';

  buildPhase = ''
    runHook preBuild
    find . -type f -exec ${dos2unix}/bin/dos2unix {} +
    echo "No build required"
    runHook postBuild
  '';

  installPhase = ''
    runHook preInstall
    mkdir -p $out/lib/cups/filter
    mkdir -p $out/share/cups/model

    cp ${./pstoufr2cpca} $out/lib/cups/filter/
    chmod +x $out/lib/cups/filter/pstoufr2cpca
    ${dos2unix}/bin/dos2unix $out/lib/cups/filter/pstoufr2cpca

    PPD_PATH=$(find . -name "*C5870*.ppd")
    if [ -n "$PPD_PATH" ]; then
      cp $PPD_PATH $out/share/cups/model/
      find $out/share/cups/model -type f -exec ${dos2unix}/bin/dos2unix {} +
    else
      echo "Error: PPD file not found"
      exit 1
    fi

    if [ -d "libs" ]; then
      cp -r libs/* $out/lib/
    elif [ -d "cndrvcups-lb-3.70/libs" ]; then
      cp -r cndrvcups-lb-3.70/libs/* $out/lib/
    else
      echo "Error: Library directory not found"
      exit 1
    fi
    find $out -type f -exec ${dos2unix}/bin/dos2unix {} +
    runHook postInstall
  '';

  postFixup = ''
    wrapProgram $out/lib/cups/filter/pstoufr2cpca \
      --prefix LD_LIBRARY_PATH : $out/lib
  '';

  meta = with lib; {
    description = "Canon UFR II / UFRII LT Printer Driver for Linux";
    homepage = "https://www.canon.it/support/consumer_products/products/fax__multifunctionals/laser/imagerunner_series/imagerunner-dx-c5870i.html";
    license = licenses.unfree;
    platforms = platforms.linux;
  };
}
The preparation script (pstoufr2cpca):
#!/bin/sh

# Set the library path to include the directory where the Canon libraries are installed
export LD_LIBRARY_PATH="/nix/store/path-to-canon-libs:$LD_LIBRARY_PATH"

# Execute the original pstoufr2cpca binary
exec /nix/store/path-to-original-pstoufr2cpca "$@"
Note: In the preparation script, /nix/store/path-to-canon-libs and /nix/store/path-to-original-pstoufr2cpca should be replaced with the actual paths in your Nix store. These paths are typically determined at build time.
The error occurs when trying to build this package. Let me know if you need any additional information or clarification about this derivation or the preparation script.
ghpzin commented 2 months ago

Your pstoufr2cpca script does something weird and package does not build as is, because this:

    cp ${./pstoufr2cpca} $out/lib/cups/filter/
    chmod +x $out/lib/cups/filter/pstoufr2cpca

needs to be:

    cp ${./pstoufr2cpca} $out/lib/cups/filter/pstoufr2cpca
    chmod +x $out/lib/cups/filter/pstoufr2cpca

${./pstoufr2cpca} would interpolate to store path, so if you cp as you do it would become something like 0nvb5i5baka2hjba4nd0v43dm932mwck-pstoufr2cpca and next line with chmod would fail.

About your error thought, I think something has windows newlines which nix can't handle. Considering it builds fine for me by copy-pasting your code (and fixing problem above), I assume it's in .nix file for that package you wrote.

The only refs I could find: https://stackoverflow.com/questions/78183326/how-to-fix-nix-build-failure-due-to-a-file-using-the-windows-way-to-break-line https://discourse.nixos.org/t/what-could-cause-this-flake-to-fail-on-my-computer-and-succeed-on-another/41868/2

Artturin commented 2 months ago

The error occurs specifically on line 1579, which is: eval "${!curPhase:-$curPhase}"

This indicates there's a problem in your phases. Closing this but you can get support in other channels.