examples/installer: Copy channels when installing
This mirrors the default for the NixOS installer.
@@ -12,6 +12,30 @@ end
MOUNT_POINT = File.realpath(ARGV.shift)
BOOT_PARTITION = File.realpath(ARGV.shift)
+CHANNELS_BUILDER = <<EOF
+(import <nixpkgs> {}).callPackage (
+ { lib
+ , runCommand
+ }:
+
+ { channels }:
+
+ runCommand "user-environment" {} ''
+ (
+ PS4=" $ "
+ set -x
+ mkdir -p $out
+ cd $out
+ ${(
+ lib.concatMapStringsSep "\n" ({ name, path }: ''
+ ln -sf ${lib.escapeShellArg (builtins.path { inherit path name; })} ${lib.escapeShellArg name}
+ '') channels
+ )}
+ )
+ ''
+) { }
+EOF
+
def step_marker(text)
puts ""
puts ":: #{text}"
@@ -59,6 +83,41 @@ unless boot_image
raise "`boot_image` build unexpectedly failed? (boot_image == #{boot_image.inspect})"
end
+step_marker "Copying channels"
+
+# We're assuming here that the NIX_PATH refs are already in the Nix store,
+# since this is how the installer sets the system up. Ideally we'd do something
+# like copy the NIX_PATH refs to the store only if they're not already in the
+# store, but that's not trivial to do.
+# Anyway channels aren't great and only used to mirror the defaults from NixOS.
+
+defexpr = File.join(MOUNT_POINT, "/root/.nix-defexpr")
+FileUtils.mkdir_p(defexpr)
+File.chmod(0700, defexpr)
+unless File.exists?(File.join(defexpr, "/channels")) or File.symlink?(File.join(defexpr, "/channels"))
+ File.symlink("/nix/var/nix/profiles/per-user/root/channels", File.join(defexpr, "/channels"))
+end
+
+# Override work that would be done by <nixpkgs/nixos/modules/services/misc/nix-daemon.nix>
+# See also `nix.nixPath` option, which uses `nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos`
+# We only support `nixos-unstable` from the master branch.
+# We don't publish "known good" channel releases yet for Mobile NixOS.
+channels = [
+ ["https://nixos.org/channels/nixos-unstable", "nixos"],
+ ["https://github.com/NixOS/mobile-nixos/archive/refs/heads/master.tar.gz", "mobile-nixos"],
+].map{|pair| pair.join(" ") }.join("\n")
+File.write(File.join(MOUNT_POINT, "/root/.nix-channels"), channels)
+
+# We need to synthesize a valid store path to put as the first channel symlink.
+channels = <<EOF
+[
+ { name = "nixos"; path = #{Nix.ensure_in_store(Nix.instantiate(expr: "<nixpkgs>", json: false))}; }
+ { name = "mobile-nixos"; path = #{Nix.ensure_in_store(Nix.instantiate(expr: "<mobile-nixos>", json: false))}; }
+]
+EOF
+channels_path = Nix.build(expr: CHANNELS_BUILDER, args: {channels: channels})
+puts "Activating channels..."
+Nix.set_profile(profile: File.join(MOUNT_POINT, "/nix/var/nix/profiles/per-user/root/channels"), set: channels_path)
step_marker "Finalizing install"
