nix-community / npmlock2nix

nixify npm based packages [maintainer=@andir]
Apache License 2.0
130 stars 42 forks source link

If the HOME environment variable is not set, npm can't build #187

Open a-h opened 1 year ago

a-h commented 1 year ago

When building a web app inside a container, the $HOME variable is not set. NPM tries to write to the non-existent directory, and you get a EACCES: permission denied, mkdir '/homeless-shelter' error.

      app = nl2nix.v2.build {
        src = ./.;
        nodejs = pkgs.nodejs-18_x;
        buildCommands = [ "npm run build" ];
      };

Example logs:

trace: warning: [npmlock2nix] You are using the new v2 beta api. The interface isn't stable yet. Please report any issues at https://github.com/nix-community/npmlock2nix/issues
these 4 derivations will be built:
  /nix/store/bqmd5qx140xxbr4j02y07k54ri2i8743-app-nodejs-0.1.0.drv
  /nix/store/7pd87dr33nncigdm5gk83sjzd81qipmd-docker-layer-app.drv
  /nix/store/qgq0pmkdgpi65ngijx4cvxmwh79cwpsb-runtime-deps.drv
  /nix/store/cx930cb5wc2zy3rw8xgfzzm284m38y8p-docker-image-app.tar.gz.drv
building '/nix/store/bqmd5qx140xxbr4j02y07k54ri2i8743-app-nodejs-0.1.0.drv'...
app-nodejs> unpacking sources
app-nodejs> unpacking source archive /nix/store/j0ll9ipg4ipyyd4fcdrwyj3y8wdnnwbh-bnfw680ps3kma0rsx19a5n1qyb1qyv5k-source
app-nodejs> source root is bnfw680ps3kma0rsx19a5n1qyb1qyv5k-source
app-nodejs> patching sources
app-nodejs> configuring
app-nodejs> no configure script, doing nothing
app-nodejs> building
app-nodejs> npm verb cli /nix/store/mdgmqgbiqw30lygdn6q5pd6cvx4ialvb-nodejs-18.16.1/bin/node /nix/store/mdgmqgbiqw30lygdn6q5pd6cvx4ialvb-nodejs-18.16.1/bin/npm
app-nodejs> npm info using npm@9.5.1
app-nodejs> npm info using node@v18.16.1
app-nodejs> npm verb cache could not create cache: Error: EACCES: permission denied, mkdir '/homeless-shelter'
app-nodejs> npm verb logfile could not create logs-dir: Error: EACCES: permission denied, mkdir '/homeless-shelter'
app-nodejs> npm verb title npm run build
app-nodejs> npm verb argv "run" "build" "--loglevel" "verbose"
app-nodejs> npm verb logfile logs-max:10 dir:/homeless-shelter/.npm/_logs/2023-07-19T12_15_14_436Z-
app-nodejs> npm verb logfile could not be created: Error: ENOENT: no such file or directory, open '/homeless-shelter/.npm/_logs/2023-07-19T12_15_14_436Z-debug-0.log'
app-nodejs> npm verb logfile no logfile created
app-nodejs> 

Workaround

A workaround is to customise the buildCommands to set the $HOME directory as part of the build process.

      app = nl2nix.v2.build {
        src = ./.;
        nodejs = pkgs.nodejs-18_x;
        buildCommands = [ "HOME=$PWD" "npm run build" ];
      };

Possible solution

If HOME is set to something that's not writable, set a sensible default automatically?