nix-book/source/recipes/devshell/nix-non-flake/main.adoc0
Amy de Buitléir 5835a15a40 temp
2025-09-16 13:38:20 +01:00

90 lines
2.2 KiB
Text

[#devshell-nix-non-flake]
= Access a non-flake package (not in nixpkgs)
////
$ mkdir tempwork
$ cd tempwork
$ cp ../flake.nix flake.nix
$ git add flake.nix
$ nix develop
$ git add flake.lock
$ git commit -a -m temp
////
In this example, we will use a nix package (not a flake) defined in a remote git repo.
However, you can use any of the flake reference styles defined in <<#flakeref>>.
The `hello-nix` https://codeberg.org/mhwombat/hello-nix[repo] provides a `default.nix`.
If the derivation in that file allows us to supply our own package set,
then our flake can call it to build `hello nix`.
If instead it requires `<nixpkgs>`, it is not pure and we cannot use it.
For example, if the file begins with an expression such as
[source,nix]
....
with (import <nixpkgs> {});
....
then it requires `nixpkgs` so we cannot use it.
Instead, we have to write our own derivation (see <<devshell-impure>>).
Fortunately the file begins with
[source,nix]
....
{ pkgs ? import <nixpkgs> {} }:
....
then it accepts a package set as an argument,
only using `<nixpkgs>` if no argument is provided.
We can use it directly to build `hello-nix` (see <<devshell-pure>>).
[#devshell-pure]
== If the nix derivation does not require nixpkgs
[source,nix,linenums,highlight='5..8,17']
.flake.nix
....
$# cat flake.nix
....
Lines 5-8 fetches the git repo for `hello-nix`.
However, it is not a flake, so we have to build it;
this is done in line 15.
Here's a demonstration using the shell.
....
$ hello-nix # Fails outside development shell
$# start-shell nix develop <<EOL
$ hello-nix
$# EOL
....
[#devshell-impure]
== If the nix derivation requires `nixpkgs`
In this case, we need to write the derivation ourselves.
We can use `default.nix` (from the hello-nix repo) as a model.
Line 15 should be replaced with:
[source,nix]
....
helloNix = pkgs.stdenv.mkDerivation {
name = "hello-nix";
src = hello-nix;
installPhase =
''
mkdir -p $out/bin
cp $src/hello-nix $out/bin/hello-nix
chmod +x $out/bin/hello-nix
'';
};
....
////
Good adoc0 scripts clean up after themselves.
$ cd .. ; rm -rf tempwork # clean up
////