nix-community / dream2nix

Simplified nix packaging for various programming language ecosystems [maintainer=@DavHau]
https://dream2nix.dev
MIT License
1.02k stars 123 forks source link

sveltekit fails to build #459

Open Suyashtnt opened 1 year ago

Suyashtnt commented 1 year ago

rt repo: https://github.com/suyashtnt/personal-website

/home/tntman/projects/personal-website〉direnv reload                                                                                                                                            01/21/2023 05:05:34 
direnv: loading ~/projects/personal-website/.envrc
direnv: using flake
warning: Git tree '/home/tntman/projects/personal-website' is dirty
[0/1 built, 0.0 MiB DL] querying nix-shell-env on https://nixpkgs-wayland.cachix.orgdirenv: ([direnv export json]) is taking a while to execute. Use CTRL-C to give up.
error: builder for '/nix/store/84gd04gqna6ys5yciw4vf8xr9ybcy3c8-__at__sveltejs__slash__kit-1.2.2.drv' failed with exit code 1;
       last 10 log lines:
       >   code: 'ERR_MODULE_NOT_FOUND'
       > }
       > npm ERR! code ELIFECYCLE
       > npm ERR! errno 1
       > npm ERR! @sveltejs/kit@1.2.2 postinstall: `node postinstall.js`
       > npm ERR! Exit status 1
       > npm ERR!
       > npm ERR! Failed at the @sveltejs/kit@1.2.2 postinstall script.
       > npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
       > 
       For full logs, run 'nix log /nix/store/84gd04gqna6ys5yciw4vf8xr9ybcy3c8-__at__sveltejs__slash__kit-1.2.2.drv'.
error: 1 dependencies of derivation '/nix/store/3qgl8ccq67zvjv6xdv5izw503jwx38x0-node-modules.drv' failed to build
error: 1 dependencies of derivation '/nix/store/0gndl28c5py74bmn1brd1i5av0cgqwcb-nix-shell-env.drv' failed to build
warning: Git tree '/home/tntman/projects/personal-website' is dirty
direnv: nix-direnv: renewed cache
direnv: export ~XDG_DATA_DIRS

image

hsjobeki commented 1 year ago

@DavHau this is an illustrative example, why we need build flags for "pre-post-/install scripts" The build fails because "@sveltejs/kit@1.2.2" requires things such as

if (!fs.existsSync('package.json')) continue;
if (!fs.existsSync('svelte.config.js')) continue;

-> https://unpkg.com/@sveltejs/kit@1.2.2/postinstall.js

But package.json as well as svelte.config.js do/would only exist in the main derivation. Not inside the dependency itself.

Fix: I must implement methods that allow granular overrides for such special cases.

cor commented 1 year ago

@hsjobeki is there any temporary workaround for this? I need to build a Svelte app with nix

hsjobeki commented 1 year ago

I‘ll have a look at it.

hsjobeki commented 1 year ago

@cor this is my flake to build a svelte app.

This is a little hacky but should get the job done. Until i find time to fix the linked issue.

{
  inputs.dream2nix.url = "github:nix-community/dream2nix";
  outputs = inp:
    inp.dream2nix.lib.makeFlakeOutputs {
      systems = ["x86_64-linux"];
      config.projectRoot = ./.;
      source = ./.;
      projects = ./projects.toml;
      packageOverrides = {
        "myapp" = {
          # run the postinstall script manually
          "runSvelteKitInstall" = {
            preInstall = ''
              node ./node_modules/@sveltejs/kit/postinstall.js
            '';
          };
        };
        # -> Disable the postinstall script
        # 
        # we'll run this manually in the root project.
        # Currently install-scripts cannot access package.json 
        # from your project. This is a known issue in dream2nix
                "@sveltejs/kit" = {
                        "disableScripts" = {
              # this should be preInstall but somehow didnt work.
              # This is fine because sveltekit has no build script
              # export isMain=true is currently a hack to disable the install scripts of a package
                            buildPhase = ''
                                export isMain=true
                            '';
                            postInstall = ''
                                export isMain=
                            '';
                        };
                    };
            };
    };
}

EDIT: Just saw that sveltekit needs the .svelte-kit folder. In particular it warns that tsconfig.ts is not found but still builds fine. You'd want to include a script that generates the .svelte-kit folder in the preBuild phase of myapp

->


 "myapp" = {
          # generate the .svelte-kit folder
          # I am not sure about the command.
          "prepare-svelte" = {
            preBuild = ''
             npx svelte-kit sync
            '';
          };
        };