trunk icon indicating copy to clipboard operation
trunk copied to clipboard

Error using trunk from a Nix derivation

Open jmc-figueira opened this issue 2 years ago • 4 comments

Similarly to #260, I'm facing an issue with running trunk in a Nix derivation:

place-mural-miei-frontend = pkgs.stdenvNoCC.mkDerivation {
  pname = "place-mural-miei-frontend";
  version = pkgVersion;

  buildInputs = [
    rust
    pkgs.trunk
    pkgs.wasm-bindgen-cli
    (import-cargo.builders.importCargo {
      lockFile = ./Cargo.lock;
      inherit pkgs;
    }).cargoHome
  ];

  src = pkgRoot;

  buildPhase = ''
    trunk build
  '';
};

The error resulting from this derivation is related to the fact that, during evaluation, Nix prevents creating directories outside the derivation output and trunk requires creating the cache directory, as such:

    Finished release [optimized] target(s) in 29.64s
Apr 12 03:09:35.631  INFO fetching cargo artifacts
Apr 12 03:09:35.696  INFO processing WASM
Apr 12 03:09:35.701 ERROR  error
error from HTML pipeline
Caused by:
    0: error from asset pipeline
    1: failed creating cache directory
    2: Permission denied (os error 13)
Error: error from HTML pipeline
Caused by:
    0: error from asset pipeline
    1: failed creating cache directory
    2: Permission denied (os error 13)

Now, I don't expect the maintainers of this project to support this very niche use case; however, I would just like some guidance on the cache directory's exact function (as it doesn't seem to be documented, hence I assume it to have some internal function which is opaque to the end user) and if it is possible to change its location at runtime in any way, as a workaround for this issue.

jmc-figueira avatar Apr 12 '22 03:04 jmc-figueira

Hello @jmc-figueira, thank you for the issue.

Trunk needs the cache folder to download additional tools required during the build process. That includes wasm-bindgen, binaryen's wasm-opt and dart-sass (if the sass/scss asset is used).

They're downloaded on the fly, extracted and then later executed. Therefore, we need a place to put them. To locate the right cache dir location, we use the directories crate (cache_dir docs).

You could try relocating the cache dir by setting the XDG_CACHE_HOME env var to wherever you need it to be for NixOS to work.

dnaka91 avatar Apr 13 '22 14:04 dnaka91

Would it be possible instead to instruct trunk to use pre-installed versions of those tools (installed by the package manager)?

I managed to work around this issue by recreating what trunk does manually through a Nix script, perhaps that might be the best option for my particular use case as of now, though.

jmc-figueira avatar Apr 13 '22 15:04 jmc-figueira

Would it be possible instead to instruct trunk to use pre-installed versions of those tools (installed by the package manager)?

If the pre-installed versions are available, it will use them instead. But that only happens, as long as the system-installed version exactly matches the expected one.

You can override the versions in Trunk.toml under the tools section, as the defaults probably don't match with what you get from NixOS.

dnaka91 avatar Apr 13 '22 16:04 dnaka91

With #358 it should now be easier to use from NixOS. You just need to make sure binaryen and sass (the dart-sass version, not sassc) are available and it won't install the tools.

I see you already add wasm-bindgen-cli, you just have to make sure it matches the project or override in the Trunk.toml as mentioned earlier.

dnaka91 avatar Apr 14 '22 03:04 dnaka91

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Oct 02 '23 00:10 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Oct 14 '23 00:10 github-actions[bot]

You could try relocating the cache dir by setting the XDG_CACHE_HOME

Thanks, that worked

Mubelotix avatar Mar 22 '24 15:03 Mubelotix