at 25.11-pre 3.9 kB view raw
1{ 2 config, 3 lib, 4 pkgs, 5 ... 6}: 7 8with lib; 9let 10 cfg = config.virtualisation.googleComputeImage; 11 defaultConfigFile = pkgs.writeText "configuration.nix" '' 12 { ... }: 13 { 14 imports = [ 15 <nixpkgs/nixos/modules/virtualisation/google-compute-image.nix> 16 ]; 17 } 18 ''; 19in 20{ 21 22 imports = [ 23 ./google-compute-config.nix 24 ./disk-size-option.nix 25 ../image/file-options.nix 26 (lib.mkRenamedOptionModuleWith { 27 sinceRelease = 2411; 28 from = [ 29 "virtualisation" 30 "googleComputeImage" 31 "diskSize" 32 ]; 33 to = [ 34 "virtualisation" 35 "diskSize" 36 ]; 37 }) 38 ]; 39 40 options = { 41 virtualisation.googleComputeImage.configFile = mkOption { 42 type = with types; nullOr str; 43 default = null; 44 description = '' 45 A path to a configuration file which will be placed at `/etc/nixos/configuration.nix` 46 and be used when switching to a new configuration. 47 If set to `null`, a default configuration is used, where the only import is 48 `<nixpkgs/nixos/modules/virtualisation/google-compute-image.nix>`. 49 ''; 50 }; 51 52 virtualisation.googleComputeImage.compressionLevel = mkOption { 53 type = types.int; 54 default = 6; 55 description = '' 56 GZIP compression level of the resulting disk image (1-9). 57 ''; 58 }; 59 60 virtualisation.googleComputeImage.contents = mkOption { 61 type = with types; listOf attrs; 62 default = [ ]; 63 description = '' 64 The files and directories to be placed in the image. 65 This is a list of attribute sets {source, target, mode, user, group} where 66 `source' is the file system object (regular file or directory) to be 67 grafted in the file system at path `target', `mode' is a string containing 68 the permissions that will be set (ex. "755"), `user' and `group' are the 69 user and group name that will be set as owner of the files. 70 `mode', `user', and `group' are optional. 71 When setting one of `user' or `group', the other needs to be set too. 72 ''; 73 example = literalExpression '' 74 [ 75 { 76 source = ./default.nix; 77 target = "/etc/nixos/default.nix"; 78 mode = "0644"; 79 user = "root"; 80 group = "root"; 81 } 82 ]; 83 ''; 84 }; 85 86 virtualisation.googleComputeImage.efi = mkEnableOption "EFI booting"; 87 }; 88 89 #### implementation 90 config = { 91 boot.initrd.availableKernelModules = [ "nvme" ]; 92 boot.loader.grub = mkIf cfg.efi { 93 device = mkForce "nodev"; 94 efiSupport = true; 95 efiInstallAsRemovable = true; 96 }; 97 98 fileSystems."/boot" = mkIf cfg.efi { 99 device = "/dev/disk/by-label/ESP"; 100 fsType = "vfat"; 101 }; 102 103 system.nixos.tags = [ "google-compute" ]; 104 image.extension = "raw.tar.gz"; 105 system.build.image = config.system.build.googleComputeImage; 106 system.build.googleComputeImage = import ../../lib/make-disk-image.nix { 107 name = "google-compute-image"; 108 inherit (config.image) baseName; 109 postVM = '' 110 PATH=$PATH:${ 111 with pkgs; 112 lib.makeBinPath [ 113 gnutar 114 gzip 115 ] 116 } 117 pushd $out 118 # RTFM: 119 # https://cloud.google.com/compute/docs/images/create-custom 120 # https://cloud.google.com/compute/docs/import/import-existing-image 121 mv $diskImage disk.raw 122 tar -Sc disk.raw | gzip -${toString cfg.compressionLevel} > \ 123 ${config.image.fileName} 124 rm disk.raw 125 popd 126 ''; 127 format = "raw"; 128 configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile; 129 inherit (cfg) contents; 130 partitionTableType = if cfg.efi then "efi" else "legacy"; 131 inherit (config.virtualisation) diskSize; 132 inherit config lib pkgs; 133 }; 134 135 }; 136 137}