Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active March 8, 2024 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/6e943084248e622b1d779a3676233d28 to your computer and use it in GitHub Desktop.
Save Mic92/6e943084248e622b1d779a3676233d28 to your computer and use it in GitHub Desktop.
Disko impure image script example

First generate the image script:

$ nix build .#nixosConfigurations.myhost.config.system.build.diskoImagesScript

Next we build the image:

$ sudo ./result --build-memory 2048

In our example the script will generate the following image:

$ ls -la vdb.raw
.rw-r--r-- root root 10 GB 2 minutes ago vdb.raw

The build script has more options as you can see here:

$ ./result --help
Usage: $script [options]

Options:
* --pre-format-files <src> <dst>
  copies the src to the dst on the VM, before disko is run
  This is useful to provide secrets like LUKS keys, or other files you need for formating
* --post-format-files <src> <dst>
  copies the src to the dst on the finished image
  These end up in the images later and is useful if you want to add some extra stateful files
  They will have the same permissions but will be owned by root:root
* --build-memory <amt>
  specify the ammount of memory that gets allocated to the build vm (in mb)
  This can be usefull if you want to build images with a more involed NixOS config
  By default the vm will get 1024M/1GB
{
"nodes": {
"disko": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1709773529,
"narHash": "sha256-CNz9ybeR88j8QQxy7YNFa8RlNq3pWnXLvocWIt2n5Mg=",
"owner": "nix-community",
"repo": "disko",
"rev": "a2009bc2b4e1d3ff5360048292deb0d610aa064b",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "disko",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1709336216,
"narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1709703039,
"narHash": "sha256-6hqgQ8OK6gsMu1VtcGKBxKQInRLHtzulDo9Z5jxHEFY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9df3e30ce24fd28c7b3e2de0d986769db5d6225d",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"disko": "disko",
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}
{
description = "A disko images example";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
disko.url = "github:nix-community/disko";
#disko.url = "git+file:/home/joerg/git/disko";
disko.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } ({ lib, ... }: {
systems = lib.systems.flakeExposed;
flake.nixosConfigurations.myhost = lib.nixosSystem {
system = "x86_64-linux";
modules = [
./simple-efi.nix
inputs.disko.nixosModules.disko
({ config, ... }: {
documentation.enable = false;
system.stateVersion = config.system.nixos.version;
})
];
};
});
}
{
disko.devices = {
disk = {
vdb = {
imageSize = "10G";
device = "/dev/disk/by-id/some-disk-id";
type = "disk";
content = {
type = "gpt";
partitions = {
ESP = {
type = "EF00";
size = "500M";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
size = "100%";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
};
};
};
};
};
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment