ralismark / nix-appimage

Convert a nixos derivation into a self-contained binary
MIT License
148 stars 17 forks source link

nix bundle only works when run with --impure #1

Closed lucasew closed 2 years ago

lucasew commented 2 years ago

If running normally, it isn't able to check reliably if the binary entry point from meta.mainProgram exists because of sandbox I guess.

How to reproduce:

nix bundle --bundler github:ralismark/nix-appimage github:lucasew/nixcfg#pkgs.wineApps.wine7zip # error: main program /nix/store/rdqaabzrxja4ib8nimxrwjn7zx65aj5d-7zip/bin/7zip does not exist
nix bundle --bundler github:ralismark/nix-appimage github:lucasew/nixcfg#pkgs.wineApps.wine7zip --impure # * works *
ralismark commented 2 years ago

Thanks for finding this issue, and for the simple reproduction instructions! Seems like it's from this assert in here: https://github.com/ralismark/nix-appimage/blob/main/flake.nix#L130

  let
    # use same auto-detect that <https://github.com/NixOS/bundlers> uses
    main =
      if drv?meta && drv.meta?mainProgram then drv.meta.mainProgram
      else (builtins.parseDrvName (builtins.unsafeDiscardStringContext drv.name)).name;
    mainPath = "${drv}/bin/${main}";
  in
  assert pkgs.lib.assertMsg (builtins.pathExists mainPath) "main program ${mainPath} does not exist"; # <-----
  mainPath;

I added it since if it didn't guess the right main program it would just silently create a broken AppImage, but I guess it doesn't work reliably in pure mode. Simple fix would be to remove the check, but I'd prefer there to still be some safeguard against that happening.

I'm not extremely familiar with nix but I can try figure something out over the next few days. Do you know any good ways of checking if a derivation has a specific file?

lucasew commented 2 years ago

Well, one workaround I can see right now is one is another derivation that depends on the first one and checks if the file exists, if Yes then create a folder in $out, if not raise an error. If that derivation is marked as a dependency to the final derivation it will run. In pure mode you cant access arbitrary nix paths, you must have that declared as some kind of dependency.

ralismark commented 2 years ago

I've pushed a fix to the develop branch that uses builtins.readDir instead, and it seems to work with your example:

nix bundle --bundler github:ralismark/nix-appimage?ref=develop github:lucasew/nixcfg#pkgs.wineApps.wine7zip

Can you try this and let me know if it fixes it?

lucasew commented 2 years ago

It works!!

Thank you, now it builds without --impure.