{ # Packages a shell script as a Nix derivation. # Try: `nix build`, then `./result/bin/greet` # Or: `nix run` description = "Package a script with stdenv.mkDerivation"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; }; outputs = { self, nixpkgs, ... }: let system = "x86_64-linux"; pkgs = import nixpkgs { inherit system; }; in { packages.${system}.default = pkgs.stdenv.mkDerivation { pname = "greet"; version = "0.1.0"; # `src = ./.` copies the flake directory into the store. # The build then runs inside that copy. src = ./.; # Skip the default unpack/build phases; this example has no C code. dontUnpack = true; dontBuild = true; # installPhase is a shell snippet. `$out` is the output store path. installPhase = '' mkdir -p $out/bin cp ${./greet.sh} $out/bin/greet chmod +x $out/bin/greet # Patch the shebang so the script uses a store-path bash, # not /bin/sh (which may not exist on a pure NixOS system). substituteInPlace $out/bin/greet \ --replace-fail "#!/usr/bin/env bash" "#!${pkgs.bash}/bin/bash" ''; }; # `nix run` looks here. `program` must be a store path to an executable. apps.${system}.default = { type = "app"; program = "${self.packages.${system}.default}/bin/greet"; meta.description = "Run the packaged greet script."; }; }; }