Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frederictobiasc committed Nov 19, 2024
0 parents commit 25099b6
Show file tree
Hide file tree
Showing 19 changed files with 1,230 additions and 0 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run nix flake check

on:
push:
pull_request:

jobs:
build:
name: Flake Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@v15
with:
diagnostic-endpoint: ""
source-url: "https://install.lix.systems/lix/lix-installer-x86_64-linux"
- uses: DeterminateSystems/magic-nix-cache-action@v8
with:
diagnostic-endpoint: ""
- run: nix flake check --log-format raw-with-logs -L

env:
FORCE_COLOR: 1
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.vscode
result*
.direnv/
.pre-commit-config.yaml
.nixos-test-history
TODO.MD

# Local debugging
img
*.raw
mnt/
tree/
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# naext: Nix Appliance Extension Tools

Extending Appliance Images.

NixOS allows for building [Appliance Images](https://nixos.org/manual/nixos/unstable/#sec-image-repart-appliance). Since Appliance Images are immutable they typically contain everything necessary to make use of such an image.

## Problem

Appliances are usually built generically. To be useful an appliance is augmented with specifics (think of configuration, programs) for a certain use case.

## Offered Solution

This project offers solutions to extend immutable appliances in a lightweight manner leveraging technologies provided by systemd and the kernel.

### `naext` Module

Allows for building extension images (`sysext`, `confext`).

#### Example

Create a confext image that provides the file `/etc/test` containing `Hello`.

```nix
naext = {
seed = "12345678-1234-1234-1234-123456789123";
extensions = {
"hello" = {
extensionType = "confext";
imageFormat = "raw";
files = {
"/etc/test".source = pkgs.writeText "example" ''Hello'';
};
};
};
};
```

## Tour

Check out:

- [Building an Image](./examples/basic.nix) with `nix-build ./examples/basic.nix`
- [Basic Integration Test](./nix/tests/basic.nix) with `nix-build ./examples/basic.nix`
- [Integration Test with verity protected extension image](./nix/tests/basic.nix) with `nix-build ./examples/basic.nix`
14 changes: 14 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
system ? builtins.currentSystem,
}:
let
sources = import ./nix/sources.nix;
pkgs = import sources.nixpkgs {
inherit system;
config.allowAliases = false;
};
outputs = import ./nix/outputs.nix { inherit pkgs; };
in
{
inherit (outputs) checks;
}
33 changes: 33 additions & 0 deletions examples/basic.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
system ? builtins.currentSystem,
}:
let
sources = import ../nix/sources.nix;
pkgs = import sources.nixpkgs {
inherit system;
config.allowAliases = false;
};
outputs = import ../nix/outputs.nix { inherit pkgs; };
in
(pkgs.lib.evalModules {
modules = [
outputs.nixosModules.default
(_: {
naext = {
seed = "12345678-1234-1234-1234-123456789123";
extensions = {
"hello" = {
extensionType = "confext";
imageFormat = "raw";
files = {
"/etc/test".source = pkgs.writeText "example" ''Hello'';
};
};
};
};
})
];
specialArgs = {
inherit pkgs;
};
}).config.naext.extensions."hello".image
144 changes: 144 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
description = "Extension Images built with Nix";

inputs = {
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
pre-commit-hooks-nix = {
url = "github:cachix/pre-commit-hooks.nix";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-compat.follows = "flake-compat";
};
};
systems.url = "github:nix-systems/default";
};
outputs =

inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;
imports = [ inputs.pre-commit-hooks-nix.flakeModule ];
flake.nixosModules = {
naext = import ./nix/module.nix;
dm-verity = import ./nix/dm-verity.nix;
};
perSystem =
{
config,
pkgs,
...
}:
{
checks =
{ }
// (import ./nix/tests {
inherit (inputs.self) nixosModules;
inherit pkgs;
enableHeavyTests = false;
});

pre-commit = {
check.enable = true;
settings = {
hooks = {
nixfmt-rfc-style.enable = true;
statix.enable = true;
};
};
};
devShells.default =
let
example-basic-mount =
pkgs.writeShellScriptBin "example-basic-mount" # bash
''
partition=p1 # assume the data partition is p1
top=$(git rev-parse --show-toplevel)
set -eux
# Build the example image and mount it as a loop device
nix-build $top/examples/basic.nix --out-link $top/result "$@"
cp -rL $top/result $top/basic.raw
loopdev=$(systemd-dissect --attach $top/basic.raw)
# Wait until the data partition becomes available
while [ ! -e "''\${loopdev}''\${partition}" ]; do
sleep 0.1 # adjust the delay as necessary
done
# Create the mount point
if [ ! -e $top/mnt ]; then
mkdir $top/mnt
fi
mount "''\${loopdev}''\${partition}" $top/mnt
'';
example-basic-umount =
pkgs.writeShellScriptBin "example-basic-umount" # bash
''
top=$(git rev-parse --show-toplevel)
set -eux
umount $top/mnt
systemd-dissect --detach $top/basic.raw
rm $top/result $top/basic.raw
'';
in
pkgs.mkShell {
shellHook = ''
${config.pre-commit.installationScript}
'';
packages = with pkgs; [
example-basic-mount
example-basic-umount
nixfmt-rfc-style
statix
util-linux
];
};
};
};
}
Loading

0 comments on commit 25099b6

Please sign in to comment.