1# Provide an initial copy of the NixOS channel so that the user
2# doesn't need to run "nix-channel --update" first.
3
4{ config, lib, pkgs, ... }:
5
6with lib;
7
8let
9 # This is copied into the installer image, so it's important that it is filtered
10 # to avoid including a large .git directory.
11 # We also want the source name to be normalised to "source" to avoid depending on the
12 # location of nixpkgs.
13 # In the future we might want to expose the ISO image from the flake and use
14 # `self.outPath` directly instead.
15 nixpkgs = lib.cleanSource pkgs.path;
16
17 # We need a copy of the Nix expressions for Nixpkgs and NixOS on the
18 # CD. These are installed into the "nixos" channel of the root
19 # user, as expected by nixos-rebuild/nixos-install. FIXME: merge
20 # with make-channel.nix.
21 channelSources = pkgs.runCommand "nixos-${config.system.nixos.version}"
22 { preferLocalBuild = true; }
23 ''
24 mkdir -p $out
25 cp -prd ${nixpkgs.outPath} $out/nixos
26 chmod -R u+w $out/nixos
27 if [ ! -e $out/nixos/nixpkgs ]; then
28 ln -s . $out/nixos/nixpkgs
29 fi
30 ${optionalString (config.system.nixos.revision != null) ''
31 echo -n ${config.system.nixos.revision} > $out/nixos/.git-revision
32 ''}
33 echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
34 echo ${config.system.nixos.versionSuffix} | sed -e s/pre// > $out/nixos/svn-revision
35 '';
36
37in
38
39{
40 # Pin the nixpkgs flake in the installer to our cleaned up nixpkgs source.
41 # FIXME: this might be surprising and is really only needed for offline installations,
42 # see discussion in https://github.com/NixOS/nixpkgs/pull/204178#issuecomment-1336289021
43 nix.registry.nixpkgs.to = {
44 type = "path";
45 path = "${channelSources}/nixos";
46 };
47
48 # Provide the NixOS/Nixpkgs sources in /etc/nixos. This is required
49 # for nixos-install.
50 boot.postBootCommands = mkAfter
51 ''
52 if ! [ -e /var/lib/nixos/did-channel-init ]; then
53 echo "unpacking the NixOS/Nixpkgs sources..."
54 mkdir -p /nix/var/nix/profiles/per-user/root
55 ${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/per-user/root/channels \
56 -i ${channelSources} --quiet --option build-use-substitutes false \
57 ${optionalString config.boot.initrd.systemd.enable "--option sandbox false"} # There's an issue with pivot_root
58 mkdir -m 0700 -p /root/.nix-defexpr
59 ln -s /nix/var/nix/profiles/per-user/root/channels /root/.nix-defexpr/channels
60 mkdir -m 0755 -p /var/lib/nixos
61 touch /var/lib/nixos/did-channel-init
62 fi
63 '';
64}