mirror of
https://codeberg.org/mhwombat/nix-book.git
synced 2026-02-03 19:48:27 +08:00
58 lines
1.6 KiB
Text
58 lines
1.6 KiB
Text
[#writeShellApplication]
|
|
= Access a top level package from the Nixpkgs/NixOS repo
|
|
|
|
In <<_introducing_a_dependency>>, we wrote a shell script that needed
|
|
access to the Linux `cowsay` command.
|
|
To accomplish this, we added a step to the `installPhase` that
|
|
modified that script by including the full path to `cowsay`.
|
|
|
|
== Using `writeShellApplication`
|
|
|
|
Let's see a different approach.
|
|
This time we won't start with an existing script;
|
|
we'll create it during installation.
|
|
The `writeShellApplication` will modify the script,
|
|
filling in the shebang and setting up the path with the dependencies specified in `runtimeInputs`.
|
|
For more information, see the
|
|
https://noogle.dev/f/pkgs/writeShellApplication[documentation].
|
|
|
|
////
|
|
$ mkdir tempwork
|
|
$ cd tempwork
|
|
$ cp ../flake.nix .
|
|
$ git add flake.nix
|
|
$ nix build
|
|
$ git add flake.lock
|
|
$ git commit -a -m temp
|
|
////
|
|
|
|
[source,nix,linenums,highlight='21..34']
|
|
.flake.nix
|
|
....
|
|
$# cat flake.nix
|
|
....
|
|
|
|
We can build the flake to see how it modifies the script.
|
|
|
|
....
|
|
$ nix build
|
|
$ cat result/bin/hello-cow
|
|
....
|
|
|
|
Here's the flake in action.
|
|
|
|
....
|
|
$ nix run
|
|
....
|
|
|
|
== Other approaches
|
|
|
|
Nix provides a variety of functions that help with common tasks such as creating scripts.
|
|
See the section on https://nixos.org/manual/nixpkgs/stable/#chap-trivial-builders[trivial build helpers]
|
|
in the Nixpkgs reference manual, particularly
|
|
https://nixos.org/manual/nixpkgs/stable/#trivial-builder-writeShellScript[writeShellScript]
|
|
and https://nixos.org/manual/nixpkgs/stable/#trivial-builder-writeShellScriptBin[writeShellScriptBin].
|
|
////
|
|
Good adoc0 scripts clean up after themselves.
|
|
$ cd .. ; rm -rf tempwork # clean up
|
|
////
|