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