nixosnixpkgs

How can I install binary file into NixOS


I'm trying to install external binary inside NixOS, using declarative ways. Inside nix-pkg manual, I found such way of getting external binary inside NixOS

{ pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation {
  name = "goss";
  src = pkgs.fetchurl {
    url = "https://github.com/aelsabbahy/goss/releases/download/v0.3.13/goss-linux-amd64";
    sha256 = "1q0kfdbifffszikcl0warzmqvsbx4bg19l9a3vv6yww2jvzj4dgb";
  };
  phases = ["installPhase"];             
  installPhase = ''
  
  '';

But I'm wondering, what should I add inside InstallPhase, to make this binary being installed inside the system?


Solution

  • This seems to be an open source Go application, so it's preferable to use Nixpkgs' Go support instead, which may be more straightforward than patching a binary.

    That said, installPhase is responsible creating the $out path; typically mkdir -p $out/bin followed by cp, make install or similar commands.

    So that's not actually installing it into the system; after all Nix derivations are not supposed to have side effects. "Installing" it into the system is the responsibility of NixOS's derivations, as configured by you.

    You could say that 'installation' is the combination of modifying the NixOS configuration + switching to the new NixOS. I tend to think about the modification to the configuration only; the build and switch feel like implementation details, even though nixos-rebuild is usually a manual operation.

    Example:

    installPhase = ''
      install -D $src $out/bin/goss
      chmod a+x $out/bin/goss
    '';
    

    Normally chmod would be done to a local file by the build phase, but we don't really need that phase here.