-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflake-module.nix
More file actions
236 lines (218 loc) · 7.2 KB
/
flake-module.nix
File metadata and controls
236 lines (218 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Flake module for oracle-cloud-nixos
#
# This module can be imported by end users to customize the package set
# and terraform version used by the devShell and packages.
#
# Usage in your flake.nix:
#
# inputs.oracle-cloud-nixos.url = "github:johnrichardrinehart/oracle-cloud-nixos";
#
# outputs = inputs@{ flake-parts, ... }:
# flake-parts.lib.mkFlake { inherit inputs; } {
# imports = [ inputs.oracle-cloud-nixos.flakeModule ];
#
# perSystem = { ... }: {
# oracle-cloud-nixos = {
# # Use a custom nixpkgs
# pkgs = import inputs.nixpkgs-custom { inherit system; };
# # Use a specific terraform version
# terraform = pkgs.terraform_1_5;
# };
# };
# };
#
{
self,
inputs,
lib,
flake-parts-lib,
...
}:
let
inherit (lib) mkOption types;
inherit (flake-parts-lib) mkPerSystemOption;
in
{
options = {
flake.flakeModule = mkOption {
type = types.deferredModule;
description = "The flake module that can be imported by downstream flakes";
readOnly = true;
};
perSystem = mkPerSystemOption (
{
config,
system,
pkgs,
...
}:
{
options.oracle-cloud-nixos = {
pkgs = mkOption {
type = types.lazyAttrsOf types.raw;
default = pkgs;
defaultText = lib.literalExpression "pkgs";
description = ''
The nixpkgs package set to use for building packages and the devShell.
Override this to use a custom nixpkgs or add overlays.
'';
};
terraform = mkOption {
type = types.package;
default =
let
pkgsWithTerraform = import inputs.nixpkgs {
inherit system;
config.allowUnfreePredicate =
pkg:
builtins.elem (config.oracle-cloud-nixos.pkgs.lib.getName pkg) [
"terraform"
];
};
in
pkgsWithTerraform.terraform;
defaultText = lib.literalExpression "pkgs.terraform (with allowUnfree)";
description = ''
The terraform package to use in the devShell.
Override this to use a specific terraform version.
'';
};
extraDevShellPackages = mkOption {
type = types.listOf types.package;
default = [ ];
description = ''
Additional packages to include in the devShell.
'';
};
ociImage = {
hostname = mkOption {
type = types.str;
default = "nixos-oci";
description = "Hostname for the NixOS OCI image";
};
stateVersion = mkOption {
type = types.str;
default = "25.11";
description = "NixOS state version for the OCI image";
};
extraModules = mkOption {
type = types.listOf types.deferredModule;
default = [ ];
description = ''
Additional NixOS modules to include in the OCI base image.
'';
};
};
};
}
);
};
config = {
# Export this module for downstream flakes to import
flake.flakeModule = {
imports = [ ./flake-module.nix ];
};
perSystem =
{ config, system, ... }:
let
cfg = config.oracle-cloud-nixos;
pkgs = cfg.pkgs;
# Wrapper so terraform always targets the terraform/ directory
terraformWrapper = pkgs.writeShellScriptBin "terraform" ''
if [ -z "$FLAKE_ROOT" ]; then
echo "error: FLAKE_ROOT not set. Are you in the devShell?" >&2
exit 1
fi
exec ${cfg.terraform}/bin/terraform -chdir="$FLAKE_ROOT/terraform" "$@"
'';
# Helper function to build OCI image for a given target system
# Can use cross-compilation when targetSystem differs from system
mkOciImage =
targetSystem:
let
nixos = inputs.nixpkgs.lib.nixosSystem {
system = targetSystem;
modules = [
# OCI image module from nixpkgs (includes fetch-ssh-keys service)
"${inputs.nixpkgs}/nixos/modules/virtualisation/oci-image.nix"
"${inputs.nixpkgs}/nixos/modules/virtualisation/oci-options.nix"
# OCI hardware support (iSCSI boot, network drivers)
./modules/oci-hardware.nix
(
{ lib, pkgs, ... }:
{
system.stateVersion = cfg.ociImage.stateVersion;
networking.hostName = cfg.ociImage.hostname;
# Disable documentation to reduce image size
documentation.enable = false;
# Enable flakes and nix-command
nix.settings.experimental-features = [
"nix-command"
"flakes"
];
# Minimal packages for administration
environment.systemPackages = with pkgs; [
vim
git
htop
];
}
)
]
++ cfg.ociImage.extraModules;
};
in
nixos.config.system.build.OCIImage;
in
{
packages = {
# Cleanup old OCI custom images (keep max 3)
cleanup-images = pkgs.writeShellApplication {
name = "cleanup-images";
runtimeInputs = with pkgs; [
oci-cli
jq
coreutils
];
text = builtins.readFile ./scripts/cleanup-images.sh;
};
# Generic OCI base image - vanilla NixOS with OCI hardware support
# Build for the target architecture matching the perSystem's system
# (aarch64-linux builds ARM image, x86_64-linux builds x86 image)
oci-base-image = mkOciImage system;
}
# Cross-compiled image: build for the opposite architecture
# On x86_64-linux: provides oci-base-image-aarch64-cross (ARM)
# On aarch64-linux: provides oci-base-image-x86_64-cross (x86)
// lib.optionalAttrs (system == "x86_64-linux") {
oci-base-image-aarch64-cross = mkOciImage "aarch64-linux";
}
// lib.optionalAttrs (system == "aarch64-linux") {
oci-base-image-x86_64-cross = mkOciImage "x86_64-linux";
};
apps.cleanup-images = {
type = "app";
program = "${config.packages.cleanup-images}/bin/cleanup-images";
};
devShells.default = pkgs.mkShell {
packages =
with pkgs;
[
direnv
oci-cli
openssl
terraformWrapper
]
++ cfg.extraDevShellPackages;
shellHook = ''
# Source .env file if it exists (for TF_VAR_* variables)
if [ -f .env ]; then
set -a
source .env
set +a
fi
'';
};
};
};
}