justinwoo / spago2nix

Generate a derivation of (old) Spago dependencies, and use them to install them into the directory structure used by Spago.
MIT License
42 stars 22 forks source link

More beginner friendly and complete example setup, please #48

Open magthe opened 2 years ago

magthe commented 2 years ago

I'm struggling with setting up a derivation for my project and I think it'd be useful to have a beginner friendly (complete) example. Or maybe you have some public project using spago2nix that you can point to?

This is what I have at the moment:

{ pkgs ? import <nixpkgs> { }) }:

with pkgs;

let spagoPkgs = import ./spago-packages.nix { inherit pkgs; };

in stdenv.mkDerivation rec {
  pname = "my-frontend";
  src = lib.cleanSource ./.;
  version = "0.1";
  buildInputs = []; # what to put here??
  buildPhase = ''
    ${spagoPkgs.installSpagoStyle} # fails because it's a directory???
    ${spagoPkgs.buildSpagoStyle}
    ${spagoPkgs.buildFromNixStore}
  '';
  installPhase = ''
    echo "what would an install phase look like? 'spago bundle-app'?"
    echo "hello" > "$out/hello"
  '';
}

and it results in

❯ nix-build
these derivations will be built:
  /nix/store/snqbfnfiimbwmxx49sv52jcs3ixvxlhm-my-frontend-0.1.drv
building '/nix/store/snqbfnfiimbwmxx49sv52jcs3ixvxlhm-my-frontend-0.1.drv'...
unpacking sources
unpacking source archive /nix/store/1yyr076qh70rmkqh8s717q0sg7d48y0h-source
source root is source
patching sources
configuring
no configure script, doing nothing
building
/nix/store/dj40kjgp5lhs55v4hc47vyrarhq4qycz-stdenv-linux/setup: line 1309: /nix/store/cdpd8vclmd9iji26qq0pd8jyd2syhndx-install-spago-style: Is a directory
builder for '/nix/store/snqbfnfiimbwmxx49sv52jcs3ixvxlhm-my-frontend-0.1.drv' failed with exit code 126
error: build of '/nix/store/snqbfnfiimbwmxx49sv52jcs3ixvxlhm-my-frontend-0.1.drv' failed
magthe commented 2 years ago

After asking for help on the PureScript Discourse I've ended up with the following

{ pkgs ? import <nixpkgs> { }) }:

with pkgs;

let spagoPkgs = import ./spago-packages.nix { inherit pkgs; };

in stdenv.mkDerivation rec {
  pname = "hu-frontend";
  src = lib.cleanSource ./.;
  version = "0.1";

  buildInputs = [
    spagoPkgs.installSpagoStyle
    spagoPkgs.buildSpagoStyle
    spagoPkgs.buildFromNixStore
    purescript
    spago
  ];

  unpackPhase = ''
    cp $src/{packages,spago}.dhall .
    cp -r $src/src .
    install-spago-style
  '';

  buildPhase = ''
    build-spago-style ./src/*.purs
    spago bundle-app --no-build --no-install
  '';

  installPhase = ''
    install -D -m644 ./index.js $out/srv/hu-frontend.js
  '';
}

It works locally but is very different from the instructions in the project's README. Are the instructions simply outdated?

pmiddend commented 2 years ago

Thank you for leaving that here, I also had major troubles getting this to work, but with your template it's working!

klntsky commented 2 years ago

Is there a complete solution that allows calling spago2nix from nix?

klntsky commented 2 years ago

Also, I'm getting [error] Directory "/" is not accessible. Permissions {readable = True, writable = False, executable = False, searchable = True} from spago on spago bundle-app --no-build --no-install step.

EDIT: the culprit was global spago cache. spago --global-cache skip helped