I'm trying to get started with a simple Hello World derivation using the Nix manual. But it's not clear to me how to go about building it.
nix-build
it
without having to modify anything global (eg pkgs/top-level/all-packages.nix
)?pkgs/top-level/all-packages.nix
?Nix ensures that package dependency specifications are complete. Under Nix, a build process will only find resources that have been declared explicitly as dependencies. There's no way it can build until everything it needs has been correctly declared. If it builds, you will know you've provided a complete declaration.
NixOS is a Linux distro built around the Nix package system. Nix is built around the idea of immutability. It makes all packages immutable by giving them their own directory identified by a hash that is derived from ALL of that package's dependencies.
You can use nix-shell as a script interpreter to allow scripts written in arbitrary languages to obtain their own dependencies via Nix. This is done by starting the script with the following lines: #! /usr/bin/env nix-shell #! nix-shell -i real-interpreter -p packages.
Note that I am running NixOs
, not sure if my answer will valid for other (non-Linux) system.
Is there somewhere I can download the source files from so I don't have to copy them line-by-line?
You could browse nixpkgs, All source files are there.
Is there a way I can
nix-build
it without having to modify anything global (eg pkgs/top-level/all-packages.nix)?
David Grayson give excellent answer already.
I would love to add some information.
nix-build
will looking for default.nix
on the current directory and the build result will symlink named result
on current working directory.
Another way to test if nix expression could build is nix-shell
which also looking for default.nix
or shell.nix
on current directory. If the build success you will get shell prompt with your packages avaliable.
Both nix-build
and nix-shell
have -I
argument that you can point to any nix
repository including remote one.
For example, if I use nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz -p hello
, nix
will download binary cache
if exists or build hello
using current master
branch expression and give me a shell which hello
is avaliable.
$ nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz -p hello
downloading ‘https://github.com/NixOS/nixpkgs/archive/master.tar.gz’... [12850/0 KiB, 1404.5 KiB/s]
unpacking ‘https://github.com/NixOS/nixpkgs/archive/master.tar.gz’...
these paths will be fetched (0.04 MiB download, 0.19 MiB unpacked):
/nix/store/s3vlmp0k8b07h0r81bn7lh24q2mqcai8-hello-2.10
fetching path ‘/nix/store/s3vlmp0k8b07h0r81bn7lh24q2mqcai8-hello-2.10’...
*** Downloading ‘https://cache.nixos.org/nar/1ax9cr6qqqqrb4pdm1mpqn7whm6alwp56dvsh5hpgs5g8rrpnjxd.nar.xz’ (signed by ‘cache.nixos.org-1’) to ‘/nix/store/s3vlmp0k8b07h0r81bn7lh24q2mqcai8-hello-2.10’...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 40364 100 40364 0 0 40364 0 0:00:01 0:00:01 --:--:-- 37099
[nix-shell:~]$ which hello
/nix/store/s3vlmp0k8b07h0r81bn7lh24q2mqcai8-hello-2.10/bin/hello
Where is pkgs/top-level/all-packages.nix?
There is NIX_PATH
enviroment variable. The nixpkgs
portion will point you to your current repository
$ echo $NIX_PATH
nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
My all-packages.nix
is located at /nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/pkgs/top-level/all-packages.nix
One option is to clone the nixpkgs repository and then build the hello package recipe provided in that repository:
git clone https://github.com/NixOS/nixpkgs
cd nixpkgs
nix-build -A hello
Doing it this way, you don't have to modify all-packages.nix
, because it already has an entry for hello
. If you do want to modify all-packages.nix
, you can find it in the nixpkgs repository that you cloned. Just take the path of the repository you cloned, (e.g. ~/nixpkgs
) and add pkgs/top-level/all-packages.nix
to get the path to all-packages.nix
. You can see a copy of that file here:
https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/all-packages.nix
When you start building your own software that is not part of nixpkgs, you might chose to write your own default.nix
file in your own repository and put a line like this in there to import nixpkgs, using the NIX_PATH
environment variable:
let
nixpkgs = import <nixpkgs> { };
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With