patos/flake.nix

153 lines
4.9 KiB
Nix
Raw Normal View History

2024-09-12 21:57:01 +02:00
{
description = "PatOS is a minimal, immutable Linux distribution specialized for the Patagia Platform.";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
2024-09-12 21:57:01 +02:00
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
patagia-agent.url = "git+ssh://git@patagia.dev/patagia/patagia-agent?ref=main";
patagia-agent.inputs.nixpkgs.follows = "nixpkgs";
2024-09-12 21:57:01 +02:00
};
2024-09-17 23:02:53 +02:00
outputs =
{
self,
flake-utils,
nixpkgs,
patagia-agent,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
(import ./overlays)
];
};
2024-09-17 23:02:53 +02:00
# Prepare an update package for the system.
mkUpdate =
nixos:
let
config = nixos.config;
in
pkgs.runCommand "update-${config.system.image.version}"
2024-09-17 23:02:53 +02:00
{
nativeBuildInputs = with pkgs; [
erofs-utils
2024-10-13 23:39:56 +02:00
zstd
];
2024-09-17 23:02:53 +02:00
}
''
mkdir -p $out
2024-10-13 23:39:56 +02:00
cp ${config.system.build.image}/${config.boot.uki.name}_${config.system.image.version}.store.raw $out/
zstd -9 ${config.system.build.uki}/${config.system.boot.loader.ukiFile} \
-o $out/${config.system.boot.loader.ukiFile}.zst
zstd -9 ${config.system.build.image}/${config.boot.uki.name}_${config.system.image.version}.store.raw \
-o $out/${config.boot.uki.name}_${config.system.image.version}.img.zst
2024-09-17 23:02:53 +02:00
'';
# Prepare a ready-to-boot disk image.
mkInstallImage =
nixos:
let
config = nixos.config;
in
pkgs.runCommand "update-${config.system.image.version}"
2024-09-17 23:02:53 +02:00
{
nativeBuildInputs = with pkgs; [
qemu
2024-10-13 23:39:56 +02:00
zstd
];
2024-09-17 23:02:53 +02:00
}
''
mkdir -p $out
2024-10-13 23:39:56 +02:00
cp ${config.system.build.image}/${config.boot.uki.name}_${config.system.image.version}.raw $out/
2024-09-17 23:02:53 +02:00
qemu-img convert -f raw -O qcow2 -C ${config.system.build.image}/${config.boot.uki.name}_${config.system.image.version}.raw $out/disk.qcow2
2024-10-13 23:39:56 +02:00
zstd -9 ${config.system.build.image}/${config.boot.uki.name}_${config.system.image.version}.store.raw \
-o $out/${config.boot.uki.name}_${config.system.image.version}.img.zst
zstd -9 ${config.system.build.uki}/${config.system.boot.loader.ukiFile} \
-o $out/${config.system.boot.loader.ukiFile}.zst
2024-09-17 23:02:53 +02:00
'';
in
{
devShells.${system}.default = pkgs.mkShell {
2024-10-13 23:39:56 +02:00
packages = with pkgs; [
erofs-utils
just
self.packages.${system}.qemu-efi
squashfs-tools-ng
2024-09-17 23:02:53 +02:00
];
};
2024-09-12 21:57:01 +02:00
packages = {
default = self.packages.${system}.patos_image;
patos_image = mkInstallImage self.nixosConfigurations.${system}.patos;
patos_update = mkUpdate self.nixosConfigurations.${system}.patos;
2024-09-12 21:57:01 +02:00
2024-10-13 23:39:56 +02:00
image = system.build;
# FIXME: only do for x86_64
# A helper script to run the disk images above.
qemu-efi = pkgs.writeShellApplication {
2024-09-17 23:02:53 +02:00
name = "qemu-efi";
2024-09-12 21:57:01 +02:00
2024-09-17 23:02:53 +02:00
runtimeInputs = [ pkgs.qemu_kvm ];
2024-09-12 21:57:01 +02:00
2024-09-17 23:02:53 +02:00
text = ''
2024-10-21 18:44:47 +02:00
set -ex
2024-11-10 14:19:29 +01:00
state="/tmp/qemu-$USER"
mkdir -p "$state"
chmod 700 "$state"
2024-09-17 23:02:53 +02:00
qemu-system-x86_64 \
2024-11-10 14:19:29 +01:00
-cpu host \
-machine q35,accel=kvm \
-m 4G \
-smp 8 \
2024-09-17 23:02:53 +02:00
-display none \
2024-11-10 14:19:29 +01:00
-chardev "stdio,id=char0,mux=on,logfile=$state/serial.log,signal=off" \
-serial chardev:char0 \
-mon chardev=char0 \
-drive "if=pflash,format=raw,unit=0,readonly=on,file=${pkgs.OVMF.firmware}" \
-drive "if=pflash,format=raw,unit=1,readonly=on,file=${pkgs.OVMF.variables}" \
-netdev id=net00,type=user,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=net00 \
"$@"
2024-09-17 23:02:53 +02:00
'';
};
};
2024-09-12 21:57:01 +02:00
nixosConfigurations = rec {
patos = nixpkgs.lib.nixosSystem {
specialArgs.pkgs = pkgs;
system = system;
modules = [
{
_module.args = {
inherit patagia-agent;
};
}
./modules/kernel
./modules/filesystems.nix
./modules/generic.nix
./modules/minimize.nix
./modules/network.nix
# ./modules/patagia-agent.nix
./modules/partitions.nix
./modules/system_overrides.nix
./modules/sysext.nix
./modules/sysupdate.nix
./modules/utils.nix
];
};
2024-09-12 21:57:01 +02:00
};
2024-09-18 00:00:34 +02:00
}
);
2024-09-12 21:57:01 +02:00
}