How to remove carriage returns from scripts in nix?

I’m currently trying to use nodePackages.live-server as a local development server, but it appears that the main bin script has windows-style line endings, which is messing up the shebang interpretation:

❯ nix-shell -p nodejs nodePackages.live-server --run live-server
env: node\r: No such file or directory

If I manually inspect the file in the nix store, I can see that it has carriage returns on each line break.

  1. Is there a way for me to fix this with an overlay? (e.g. is there a nix function that could sanitize / convert the source code?)
  2. Alternatively, could anyone reccomend a different local development server for very simple html projects? (python -m http.server almost does it for me, but I want hot reloading).

Thanks!

1 Like

It’s strange that a script has windows line endings… I cannot check right now, anyway probably you can get rid of them using cat script | tr -d '\r'

2 Likes

Thank you!

I was able to fix with the following overlay:

      pkgs = import nixpkgs {
        inherit system;
        overlays = [
          (final: prev: {
            nodePackages.live-server = prev.nodePackages.live-server.overrideAttrs (old: {
              postInstall = ''
                fpath="$out/lib/node_modules/live-server/live-server.js"
                cat "$fpath" | tr -d '\r' > temp
                mv temp "$fpath"
                chmod u+x "$fpath"
                patchShebangs "$fpath"
              '';
            });
          })
        ];
      };

I agree this is very weird…

I think the issue is upstream with the files that were uploaded to NPM. If I manually download the tarball from NPM the script still has the windows style endings.

Interestingly, if I manually download the source code from GitHub, or if I run this package using npx I don’t encounter this issue. But, if I override the nix package to use fetchFromGitHub instead of npm for the src, the windows style newlines are still there.

It also seems like although the script ends up in bin by the end of the derivation, it isn’t being picked up by the node2nix hook that patches the script interpreter paths.

2 Likes

Could this have something to do with the git setting that automatically converts newlines to \r\n? It is called core.autocrlf and I think some windows git installers have this enabled by default.

2 Likes

Yes, this is the problem. It happened to me every time I run nix develop and it didn’t matter if I removed the carriage return from the files, git added them back. I simply ran git config core.autocrlf false and that solved the issue. In fact, git alerted me about this with the message warning: in the working copy of 'file.nix', LF will be replaced by CRLF the next time Git touches it but i didn’t realized my scripts were touching the file with git when I run nix develop.