@@ -42,125 +42,3 @@ Options:
42
42
run with set -x
43
43
```
44
44
45
- ## Generating Disk Images with Secrets Included using Disko
46
-
47
- Using Disko on NixOS allows you to efficiently create ` .raw ` VM images from a
48
- system configuration. The generated image can be used as a VM or directly
49
- written to a physical drive to create a bootable disk. Follow the steps below to
50
- generate disk images:
51
-
52
- ### Generating the ` .raw ` VM Image
53
-
54
- 1 . ** Create a NixOS configuration that includes the disko and the disk
55
- configuration of your choice**
56
-
57
- In the this example we create a flake containing a nixos configuration for
58
- ` myhost ` .
59
-
60
- ``` nix
61
- # save this as flake.nix
62
- {
63
- description = "A disko images example";
64
-
65
- inputs = {
66
- nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
67
- disko.url = "github:nix-community/disko";
68
- disko.inputs.nixpkgs.follows = "nixpkgs";
69
- };
70
-
71
- outputs = { self, disko, nixpkgs }: {
72
- nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
73
- system = "x86_64-linux";
74
- modules = [
75
- # You can get this file from here: https://github.com/nix-community/disko/blob/master/example/simple-efi.nix
76
- ./simple-efi.nix
77
- disko.nixosModules.disko
78
- ({ config, ... }: {
79
- # shut up state version warning
80
- system.stateVersion = config.system.nixos.version;
81
- # Adjust this to your liking.
82
- # WARNING: if you set a too low value the image might be not big enough to contain the nixos installation
83
- disko.devices.disk.vdb.imageSize = "10G";
84
- })
85
- ];
86
- };
87
- };
88
- }
89
- ```
90
-
91
- 2 . ** Build the disko image script:** Replace ` myhost ` in the command below with
92
- your specific system configuration name:
93
-
94
- ``` console
95
- nix build .#nixosConfigurations.myhost.config.system.build.diskoImagesScript
96
- ```
97
-
98
- 3 . ** Execute the disko image script:** Execute the generated disko image script.
99
- Running ` ./result --help ` will output the available options:
100
-
101
- ``` console
102
- ./result --help
103
- Usage: $script [options]
104
-
105
- Options:
106
- * --pre-format-files <src> <dst>
107
- copies the src to the dst on the VM, before disko is run
108
- This is useful to provide secrets like LUKS keys, or other files you need for formating
109
- * --post-format-files <src> <dst>
110
- copies the src to the dst on the finished image
111
- These end up in the images later and is useful if you want to add some extra stateful files
112
- They will have the same permissions but will be owned by root:root
113
- * --build-memory
114
- specify the ammount of memory that gets allocated to the build vm (in mb)
115
- This can be usefull if you want to build images with a more involed NixOS config
116
- By default the vm will get 1024M/1GB
117
- * --write-to-disk </dev/disk>
118
- use an actuall disk instead of writing to a file
119
- This only works if your conifg has only one disk specified
120
- There is no check if the specified path is actually a disk so you can also write to another file
121
- ```
122
-
123
- An example run may look like this:
124
-
125
- ```
126
- sudo ./result --build-memory 2048
127
- ```
128
-
129
- The script will generate the actual image outside of the nix store in the
130
- current working directory. The create image names depend on the names used in
131
- ` disko.devices.disk ` attrset in the NixOS configuration. In our code example it will
132
- produce the following image:
133
-
134
- ```
135
- $ ls -la vdb.raw
136
- .rw-r--r-- root root 10 GB 2 minutes ago vdb.raw
137
- ```
138
-
139
- ### Additional Configuration
140
-
141
- - For virtual drive use, define the image size in your Disko configuration:
142
-
143
- ``` console
144
- disko.devices.disk.<drive>.imageSize = "32G"; # Set your preferred size
145
- ```
146
-
147
- - If the ` .raw ` image size is not optimal, use ` --write-to-disk ` to write
148
- directly to a drive. This bypasses the ` .raw ` file generation, which saves on
149
- read/write operations and is suitable for single disk setups.
150
-
151
- ### Understanding the Image Generation Process
152
-
153
- 1 . Files specified in ` --pre-format-files ` and ` --post-format-files ` are
154
- temporarily copied to ` /tmp ` .
155
- 2 . Files are then moved to their respective locations in the VM both before and
156
- after the Disko partitioning script runs.
157
- 3 . The NixOS installer is executed, having access only to ` --post-format-files ` .
158
- 4 . Upon installer completion, the VM is shutdown, and the ` .raw ` disk files are
159
- moved to the local directory.
160
-
161
- > ** Note** : The auto-resizing feature is currently not available in Disko.
162
- > Contributions for this feature are welcomed. Adjust the ` imageSize `
163
- > configuration to prevent issues related to file size and padding.
164
-
165
- By following these instructions and understanding the process, you can smoothly
166
- generate disk images with Disko for your NixOS system configurations.
0 commit comments