I’m trying to prebuild an image for google cloud with certain things installed and certain files present (custom packages, configuration, public keys, …). Building the image goes fine and using a separate configuration.nix
for the guest works, but once I unpack the raw.tar.gz
and mount it, only the configuration.nix
is present but none of the packages therein nor custom files.
This is what I use to build the image
#/usr/bin/env bash
# From https://github.com/NixOS/nixpkgs/blob/5477e09999685a1468b65caea46b558e2245ee5d/nixos/maintainers/scripts/gce/create-gce.sh
nix-build '<nixpkgs/nixos/lib/eval-config.nix>' \
-A config.system.build.googleComputeImage \
--arg modules "[ (import /home/michael/tmp/nixos-gce/configuration.nix {}) <nixpkgs/nixos/modules/virtualisation/google-compute-image.nix> ]" \
--argstr system x86_64-linux \
-o gce \
-j 10
# Mount the raw in the TAR with
# https://www.baeldung.com/linux/img-raw-image-dump-file-management
# fdisk --list disk.raw
# START_PARTION=$(( START_PARTITION*SECTOR_SIZE ))
# sudo losetup --offset $START_PARTITION /dev/loop3 disk.raw
# mkdir target
# sudo mount /dev/loop3 target
This is the host configuration.nix
to point to a custom guest-configuration.nix
{ ... }:
{
imports = [
<nixpkgs/nixos/modules/virtualisation/google-compute-image.nix>
];
virtualisation.googleComputeImage.configFile = "${/home/michael/tmp/nixos-gce/guest-configuration.nix}";
}
and the guest-configuration.nix
(simplified)
{ pkgs, lib, ... }:
{
imports = [
<nixpkgs/nixos/modules/virtualisation/google-compute-config.nix>
<nixpkgs/nixos/modules/virtualisation/google-compute-image.nix>
lanzaboote.nixosModules.lanzaboote
];
# This file should end up in the image
environment.etc."test-file.txt" = {
enable = true;
source = ./this-test.txt;
};
# /home/user should be created and exist on the image
users.groups.user = {};
users.users.user = {
isNormalUser = true;
createHome = true;
home = "/home/user";
extraGroups = [
"google-sudoers"
];
authorizedKeys.keys = [
"ssh-ed25519 CUSTOM_KEY_HERE"
];
};
}