matthewbauer / nix-bundle

Bundle Nix derivations to run anywhere!
MIT License
639 stars 46 forks source link

Alternate commands #74

Open tomberek opened 3 years ago

tomberek commented 3 years ago

The proposed interface in the RFC exposed a -c, --command to allow for program names other than the default. I got something like this working, but not sure if there's a better way.

PROGRAM=mb-util nix bundle nixpkgs#mbutil --bundler .#defaultBundler
{
  description = " description";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    nix-bundle.url = "github:matthewbauer/nix-bundle";
  };
  outputs = { self, nixpkgs, nix-bundle  }:
    {
    bundlers = {
      nix-bundle = { program, system }: let
        nixpkgs' = nixpkgs.legacyPackages.${system};
        nix-bundl = import nix-bundle { nixpkgs = nixpkgs'; };
        envProg = builtins.getEnv "PROGRAM";
        prog = if envProg == ""
              then program
              else "${builtins.dirOf program}/${envProg}";
        script = nixpkgs'.writeScript "startup" ''
          #!/bin/sh
          .${nix-bundl.nix-user-chroot}/bin/nix-user-chroot -n ./nix -- "${prog}"
        '';
          #${program} "$@"
      in nix-bundl.makebootstrap {
        targets = [ script ];
        startup = ".${builtins.unsafeDiscardStringContext script} '\"$@\"'";
      };
    };

    defaultBundler = self.bundlers.nix-bundle;
    }
    ;
}
tomberek commented 3 years ago

Slightly less hacky:

{
  description = " description";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    nix-bundle.url = "github:matthewbauer/nix-bundle";
  };
  outputs = { self, nixpkgs, nix-bundle  }:
    {
    bundlers = {
      nix-bundle = { program, system }: let
        legacy = nixpkgs.legacyPackages.${system};
        nix-bundle-prog = import nix-bundle { nixpkgs = legacy; };
        script = legacy.writeScript "startup" ''
          #!/bin/sh
          PROG="$1"
          shift
          .${nix-bundle-prog.nix-user-chroot}/bin/nix-user-chroot -n ./nix -- "${builtins.dirOf program}/$PROG" "$@"
        '';
      in nix-bundle-prog.makebootstrap {
        targets = [ script ];
        startup = ".${builtins.unsafeDiscardStringContext script} '\"$@\"'";
      };
    };
    defaultBundler = self.bundlers.nix-bundle;
    };
}