Browse Source

Bootloader, activation and ISO generation

Signed-off-by: Magic_RB <magic_rb@redalder.org>
master
Magic_RB 1 month ago
parent
commit
ee6c1f8670
Signed by: Magic_RB GPG Key ID: 08D5287CC5DDCA0E
9 changed files with 166 additions and 47 deletions
  1. +36
    -0
      lib/make-bootloader/default.nix
  2. +10
    -0
      lib/make-bootloader/init.sh
  3. +8
    -1
      lib/make-initramfs.nix
  4. +40
    -0
      lib/make-iso-image.nix
  5. +19
    -19
      modules/activation/activation.sh
  6. +6
    -8
      modules/activation/default.nix
  7. +37
    -11
      modules/bootloader/default.nix
  8. +8
    -8
      overlay/bootloader-linux.nix
  9. +2
    -0
      overlay/tiny-linux.nix

+ 36
- 0
lib/make-bootloader/default.nix View File

@ -0,0 +1,36 @@
{ system, pkgs
, writeTextFile
, runCommandNoCC
, bootloaderLinux
, nglib
, name ? "bootloader"
, kernelExtraConfig ? {}
}:
let
init = runCommandNoCC "${name}-init"
{ nativeBuildInputs = [ pkgs.busybox ];
inherit (pkgs) busybox bash;
}
''
mkdir -p $out
substituteAll ${pkgs.writeShellScript "init" (builtins.readFile ./init.sh)} $out/init
chmod +x $out/init
'';
linux = bootloaderLinux {
extraConfig = kernelExtraConfig;
initramfs = nglib.makeInitramfs {
name = "initramfs.cpio";
path = nglib.makeBundle { path = init; name = "init"; };
compress = false;
};
};
in
runCommandNoCC name (with pkgs; {
inherit busybox;
nativeBuildInputs = [ pkgs.busybox ];
})
''
cp ${linux}/bzImage $out
''

+ 10
- 0
lib/make-bootloader/init.sh View File

@ -0,0 +1,10 @@
export PATH=$PATH:@bash@/bin:@busybox@/bin
# mkdir -p /dev /proc /sys /run
# mount -n -t devtmpfs devtmpfs /dev
# mount -n -t proc proc /proc
# mount -n -t sysfs sysfs /sys
# mount -n -t tmpfs tmpfs /run
echo "Hello, world!"
# bash -c 'echo "sdfdsaf"'

+ 8
- 1
lib/make-initramfs.nix View File

@ -1,7 +1,9 @@
{ system
, runCommandNoCC
, findutils, cpio, gzip
, path, name
}:
{ path, name
, compress ? true
}:
runCommandNoCC name
{ nativeBuildInputs = [
@ -10,6 +12,11 @@ runCommandNoCC name
gzip
];
}
(if compress then
''
( cd ${path} ; find . | cpio -o -H newc --quiet | gzip -9 ) > $out
''
else
''
( cd ${path} ; find . | cpio -o -H newc --quiet ) > $out
'')

+ 40
- 0
lib/make-iso-image.nix View File

@ -0,0 +1,40 @@
{ nglib, writeShellScript
, busybox, utillinux, dosfstools, e2fsprogs, parted, grub2
, system
}:
assert system.config.bootloader.enable;
nglib.runInVm {
preProcess = writeShellScript "script"
''
export PATH=${busybox}/bin:${parted}/bin
set -e
dd if=/dev/zero of=$xchg/out/image.img bs=1024 count=524288
parted --script $xchg/out/image.img \
mklabel msdos \
mkpart primary 1MiB 100%
'';
script = writeShellScript "script"
''
export PATH=${dosfstools}/bin:${e2fsprogs}/bin:${utillinux}/bin:${busybox}/bin:${parted}/bin:${grub2}/bin
set -e
_root=$(mktemp -d)
_loopdev=$(losetup -f)
losetup -P $_loopdev $out/image.img
mkfs.ext4 ''${_loopdev}p1
mount ''${_loopdev}p1 $_root
cp -r ${system.bundle}/* $_root
mkdir -p $_root/boot $_root/etc
ln -sfn /proc/mounts $_root/etc/mtab
grub-install --root-directory=$_root --boot-directory=$_root/boot --target=i386-pc /dev/loop0
'';
}

+ 19
- 19
modules/activation/activation.sh View File

@ -1,25 +1,25 @@
# Check whether NixOS or another distro is installed
ANOTHER_DISTRO=$(
if [[ -f /etc/os-release ]] ; then
source /etc/os-release
# ANOTHER_DISTRO=$(
# if [[ -f /etc/os-release ]] ; then
# source /etc/os-release
if [[ -e "$NAME" "NixSmth" ]] && [[ -e "$ID" "nixsmth" ]] ; then
false
echo $?
else
true
echo $?
fi
else
false
echo $?
fi
)
# if [[ -e "$NAME" "NixSmth" ]] && [[ -e "$ID" "nixsmth" ]] ; then
# false
# echo $?
# else
# true
# echo $?
# fi
# else
# false
# echo $?
# fi
# )
if [[ "$ANOTHER_DISTRO" == "0" ]] ; then
echo "Refusing to activate when another distro is installed!"
exit 1
fi
# if [[ "$ANOTHER_DISTRO" == "0" ]] ; then
# echo "Refusing to activate when another distro is installed!"
# exit 1
# fi
# ${concatMapStringsSep "\n"
# (file:


+ 6
- 8
modules/activation/default.nix View File

@ -1,4 +1,4 @@
{ pkgs, lib, config, ... }:
{ pkgs, lib, config, nglib, ... }:
with lib;
let
cfg = config.activation;
@ -18,12 +18,10 @@ in
};
config.activation = mkIf cfg.enable {
script = pkgs.runCommandNoCCLocal "activation" (with pkgs; {
nativeBuildInputs = [ pkgs.busybox ];
})
''
substituteAll ${pkgs.writeShellScript "activation" (builtins.readFile ./activation.sh)} $out
chmod +x $out
'';
script = nglib.writeSubstitutedShellScript {
name = "activation";
file = ./activation.sh;
substitutes = {};
};
};
}

+ 37
- 11
modules/bootloader/default.nix View File

@ -2,24 +2,50 @@
with lib;
let
cfg = config.bootloader;
initrdAlgos = lib.genAttrs [
"gzip" "bzip2" "lzma" "xz" "lzo" "lz4"
] (algo: "CONFIG_RD_${lib.toUpper algo}");
filesystems = lib.genAttrs [
"ext4" "ext3" "vfat" "xfs" "btrfs" "f2fs" "zfs"
] (fs:
if fs == "zfs" then
throw "Not supported :) would need a patch"
else
"CONFIG_${lib.toUpper fs}_FS"
);
in
{
options.bootloader = {
enable = mkEnableOption "Enable the bootloader";
config = mkOption {
kernelExtraConfig = mkOption {
description = "";
type = types.submodule {
options = {
kernelConfiguration = mkOption {
description = "";
type = types.attrsOf (types.enum ["y" "n" "m"]);
default = {};
};
};
};
type = types.attrsOf (types.enum ["y" "n" "m"]);
default = {};
};
initrdCompression = mkOption {
description = "Supported compression algorithms for initrd";
type = types.listOf (types.enum (builtins.attrNames initrdAlgos));
default = [];
};
filesystems = mkOption {
description = "Supported filesystems for initrd";
type = types.listOf (types.enum (builtins.attrNames filesystems));
default = [];
};
};
config.bootloader = mkIf cfg.enable {};
config.bootloader = mkIf cfg.enable {
kernelExtraConfig =
(builtins.listToAttrs
(map (algo:
{ name = initrdAlgos."${algo}"; value = "y"; }
) cfg.initrdCompression
++ map (fs:
{ name = filesystems."${fs}"; value = "y"; }
) cfg.filesystems));
};
}

+ 8
- 8
overlay/bootloader-linux.nix View File

@ -1,8 +1,10 @@
{ extraConfig ? {}
, callPackage
{ callPackage
, lib
, tinyLinux
}:
{ initramfs
, extraConfig ? {}
}:
tinyLinux.override {
extraConfig = {
# EFI boot and EFI stub
@ -33,14 +35,12 @@ tinyLinux.override {
# Debug logs during boot are always handy
CONFIG_PRINTK="y";
CONFIG_RD_GZIP="y";
# Initrd things, perhaps make the compression configurable?
CONFIG_BLK_DEV_INITRD="y";
CONFIG_RD_GZIP="n";
CONFIG_RD_BZIP2="n";
CONFIG_RD_LZMA="n";
CONFIG_RD_XZ="n";
CONFIG_RD_LZO="n";
CONFIG_RD_LZ4="n";
CONFIG_INITRAMFS_SOURCE=''"${initramfs}"'';
# CONFIG_INITRAMFS_COMPRESSION="gzip";
# CONFIG_INITRAMFS_COMPRESSION_GZIP="y";
# Required for shebangs
CONFIG_BINFMT_ELF="y"; # general execution


+ 2
- 0
overlay/tiny-linux.nix View File

@ -55,6 +55,8 @@ let
ls build
'';
buildPhase = "";
postInstall = "";
nativeBuildInputs = old.nativeBuildInputs ++ [ pkg-config ncurses ];


Loading…
Cancel
Save