Skip to content

Docker + Modular Kernel Config Support #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
21 changes: 10 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ RUN apt-get update && apt-get install -y \
curl git sudo qemu-system-x86 qemu-utils \
debian-archive-keyring systemd-boot reprepro xz-utils

RUN adduser --disabled-password --gecos '' nix && \
echo "nix ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/nix && \
chmod 0440 /etc/sudoers.d/nix
RUN echo "ubuntu ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/ubuntu && \
chmod 0440 /etc/sudoers.d/ubuntu

COPY --chown=nix:nix . /home/nix/mkosi
RUN mkdir -p /home/nix/mkosi/mkosi.packages /home/nix/mkosi/mkosi.cache \
/home/nix/mkosi/mkosi.builddir /home/nix/mkosi/build /nix && \
chown -R nix:nix /home/nix/mkosi /nix
COPY --chown=ubuntu:ubuntu . /home/ubuntu/mkosi
RUN mkdir -p /home/ubuntu/mkosi/mkosi.packages /home/ubuntu/mkosi/mkosi.cache \
/home/ubuntu/mkosi/mkosi.builddir /home/ubuntu/mkosi/build /nix && \
chown -R ubuntu:ubuntu /home/ubuntu/mkosi /nix

USER nix
USER ubuntu
RUN curl -L https://nixos.org/nix/install | sh -s -- --no-daemon && \
mkdir -p ~/.config/nix ~/.cache/mkosi/ && \
echo 'experimental-features = nix-command flakes' > ~/.config/nix/nix.conf
RUN /home/nix/.nix-profile/bin/nix develop -c /bin/true

WORKDIR /home/nix/mkosi
ENTRYPOINT ["/home/nix/.nix-profile/bin/nix", "develop", "-c", "/bin/bash"]
WORKDIR /home/ubuntu/mkosi
RUN /home/ubuntu/.nix-profile/bin/nix develop -c /bin/true
ENTRYPOINT ["/home/ubuntu/.nix-profile/bin/nix", "develop", "-c", "/bin/bash"]
11 changes: 9 additions & 2 deletions base/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Release=trixie

[Build]
PackageCacheDirectory=mkosi.cache
Environment=KERNEL_IMAGE KERNEL_VERSION

[Output]
Format=uki
Expand All @@ -23,7 +22,7 @@ SkeletonTrees=base/mkosi.skeleton
FinalizeScripts=base/debloat.sh
PostInstallationScripts=base/debloat-systemd.sh
PostInstallationScripts=base/efi-stub.sh
BuildScripts=base/mkosi.build
BuildScripts=kernel/mkosi.build

CleanPackageMetadata=true
Packages=kmod
Expand All @@ -44,3 +43,11 @@ BuildPackages=build-essential
pkg-config
clang
cargo
flex
bison
elfutils
bc
perl
gawk
zstd
libssl-dev
11 changes: 0 additions & 11 deletions base/mkosi.build

This file was deleted.

1 change: 0 additions & 1 deletion buildernet/buildernet.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ BuildPackages=libleveldb-dev
zlib1g-dev
libzstd-dev
libpq-dev
libssl-dev
protobuf-compiler
10 changes: 0 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
kernel = import ./kernel.nix { inherit pkgs; };
reprepro = pkgs.stdenv.mkDerivation rec {
name = "reprepro-${version}";
version = "4.16.0";
Expand All @@ -32,17 +31,8 @@
] ++ [ reprepro ];
};
in {
packages.${system} = {
kernel = kernel;
default = kernel;
};

devShells.${system}.default = pkgs.mkShell {
nativeBuildInputs = [ pkgs.qemu mkosi ];

KERNEL_IMAGE = "${kernel}/bzImage";
KERNEL_VERSION = kernel.version;

shellHook = ''
mkdir -p mkosi.packages mkosi.cache mkosi.builddir
'';
Expand Down
55 changes: 0 additions & 55 deletions kernel.nix

This file was deleted.

File renamed without changes.
58 changes: 58 additions & 0 deletions kernel/mkosi.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
set -euo pipefail

# Configuration
KERNEL_VERSION="6.13.12"
KERNEL_REPO="https://github.com/gregkh/linux"
BASE_CONFIG="$SRCDIR/kernel/kernel-yocto.config"
SNIPPETS_DIR="$SRCDIR/kernel/snippets"

echo "Building kernel $KERNEL_VERSION with snippets: ${KERNEL_CONFIG_SNIPPETS:-none}"

# Generate final config
config_file=$(mktemp)
cp "$BASE_CONFIG" "$config_file"
if [[ -n "${KERNEL_CONFIG_SNIPPETS:-}" ]]; then
IFS=',' read -ra snippets <<< "$KERNEL_CONFIG_SNIPPETS"
for snippet in "${snippets[@]}"; do
snippet_file="$SNIPPETS_DIR/${snippet}.config"
[[ -f "$snippet_file" ]] && cat "$snippet_file" >> "$config_file"
done
fi

# Calculate cache key and paths
config_hash=$(sha256sum "$config_file" | cut -d' ' -f1 | cut -c1-12)
cache_dir="$BUILDDIR/kernel-${KERNEL_VERSION}-${config_hash}"
kernel_file="$cache_dir/bzImage"

# Use cached kernel if available
if [[ -f "$kernel_file" ]]; then
echo "Using cached kernel: $kernel_file"
else
echo "Building kernel from source..."
build_dir="$BUILDROOT/build/kernel-${KERNEL_VERSION}"

# Clone if needed
[[ ! -d "$build_dir" ]] && git clone --depth 1 --branch "v${KERNEL_VERSION}" "$KERNEL_REPO" "$build_dir"

# Build kernel
cd "$build_dir"
cp "$config_file" .config
export KBUILD_BUILD_TIMESTAMP="$(date -u -d @${SOURCE_DATE_EPOCH:-$(date +%s)})"
export KBUILD_BUILD_USER="mkosi" KBUILD_BUILD_HOST="mkosi-builder"

mkosi-chroot make olddefconfig
mkosi-chroot make -j "$(nproc 2>/dev/null || echo 2)" bzImage ARCH=x86_64 CONFIG_EFI_STUB=y

# Cache result
mkdir -p "$cache_dir"
cp arch/x86_64/boot/bzImage "$cache_dir/"
cp .config "$cache_dir/config"
fi

# Install kernel
mkdir -p "$DESTDIR/usr/lib/modules/$KERNEL_VERSION"
cp "$kernel_file" "$DESTDIR/usr/lib/modules/$KERNEL_VERSION/vmlinuz"
rm -f "$config_file"

echo "Kernel installed successfully"