Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mention substituteAll in the trivial builders docs #14

Open
milahu opened this issue Nov 24, 2023 · 0 comments
Open

mention substituteAll in the trivial builders docs #14

milahu opened this issue Nov 24, 2023 · 0 comments

Comments

@milahu
Copy link
Owner

milahu commented Nov 24, 2023

substituteAll

https://discourse.nixos.org/t/how-pkgs-substituteall-chmod-x-buildenv/8167/2

Turns out pkgs.substituteAll has some extra functionality so pkgs.writeScript* isn’t needed even. So after some tinkering, this works:

let

  pkgs = import <nixpkgs> {};
  inherit (pkgs) lib;

  options = {
    foo = "foo";
    bar = "bar";
  };

  scripts = lib.mapAttrs (name: type: pkgs.substituteAll ({
    src = ./scripts + "/${name}";
    dir = "bin";
    isExecutable = true;
  } // options)) (builtins.readDir ./scripts);


  joined = pkgs.symlinkJoin {
    name = "scripts";
    paths = lib.attrValues scripts;
  };

in joined

simple example

cowsay-hello.nix

{ lib
, substituteAll
, runtimeShell
, cowsay
}:

substituteAll {
  name = "cowsay-hello";
  src = ./cowsay-hello.sh;
  dir = "bin";
  isExecutable = true;
  inherit runtimeShell;
  path = lib.makeBinPath [ cowsay ];
  postInstall = ''
    # ...
  '';
}

cowsay-hello.sh

#! @runtimeShell@

if [ -x "@runtimeShell@" ]; then export SHELL="@runtimeShell@"; fi;

export PATH=@path@:$PATH

exec cowsay hello

see also
nixpkgs/pkgs/os-specific/linux/nixos-rebuild/default.nix
nixpkgs/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh
... and many other examples in nixpkgs

trivial builders docs

https://ryantm.github.io/nixpkgs/builders/trivial-builders/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant