imagemin / imagemin-optipng

optipng plugin for imagemin
MIT License
163 stars 36 forks source link

Provide env var for passing optipng binary path? #28

Closed AlexChalk closed 4 years ago

AlexChalk commented 4 years ago

I'm on nixos, where it looks like the regular linux binary from optipng-bin fails to execute. For reference, here's how the binary is compiled on nixos: https://github.com/NixOS/nixpkgs/blob/nixos-20.09/pkgs/tools/graphics/optipng/default.nix

I see that this package (in my case) downloads a precompiled binary using optipng-bin and returns the path to the binary: https://github.com/imagemin/imagemin-optipng/blob/d439590aadb1ba5e7101f36cc2a8dcf34b0568bb/index.js#L57-L61

Are you ok with allowing users to specify an override of this path? I'm imagining using it something like this: OPTIPNG_EXECUTABLE_PATH=path/to/optipng yarn start.

Happy to provide the PR if you approve of this strategy!

AlexChalk commented 4 years ago

That said, if I can get this working, I guess I might run into the same problem with mozjpeg and other imagemin packages: https://github.com/NixOS/nixpkgs/blob/nixos-20.09/pkgs/applications/graphics/mozjpeg/default.nix

AlexChalk commented 4 years ago

I've taken a closer look, and the above solution would indeed involve passing many paths.

Instead, I ended up building a nix sandbox to run these binaries that seems to be working ok, I have the code below in default.nix in my project root, then I run build commands using nix-shell --argstr command "build command":

{ command ? "bash" }:

with import <nixpkgs> {};

(pkgs.buildFHSUserEnv {
  name = "website-npm-deps";
  targetPkgs = pkgs: (with pkgs;
  [
    # For imagemin binaries
    zlib
    libpng
  ]);

  runScript = ''
    #!@shell@
    exec ${command}
  '';
}).env

Closing as this is a good solution for my use case :).