Skip to content

Commit

Permalink
Make module for installer
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Kharin <[email protected]>
  • Loading branch information
remimimimimi committed Sep 14, 2023
1 parent 06dd0df commit 542ccb1
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 59 deletions.
12 changes: 5 additions & 7 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ in {

# NOTE: Currently supports configuration that generates raw-efi image using nixos-generators
installer = {
name,
systemImgCfg,
modules ? [],
userCode ? "",
}: let
system = systemImgCfg.config.nixpkgs.hostPlatform.system;
system = systemImgCfg.nixpkgs.hostPlatform.system;

pkgs = import nixpkgs {inherit system;};
systemImgDrv = systemImgCfg.config.system.build.${systemImgCfg.config.formatAttr};

installerScript = import ../modules/installer/installer.nix {
inherit pkgs;
systemImgDrv = "${systemImgDrv}/nixos.img";
inherit (pkgs) runtimeShell;
inherit userCode;
};

installerImgCfg = lib.nixosSystem {
Expand All @@ -46,17 +45,16 @@ in {
})

{
# TODO
environment.systemPackages = [installerScript];
environment.loginShellInit = ''
${installerScript}/bin/ghaf-installer
installer.sh
'';
}
]
++ (import ../modules/module-list.nix)
++ modules;
};
in {
name = "${name}-installer";
inherit installerImgCfg system;
installerImgDrv = installerImgCfg.config.system.build.${installerImgCfg.config.formatAttr};
};
Expand Down
11 changes: 11 additions & 0 deletions modules/installer/builtin/flush.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{...}: {
ghaf.installer.installerModules.flushImage = {
requestCode = ''
lsblk
read -p "Device name [e.g. sda]: " DEVICE_NAME
'';
providedVariables = {
deviceName = "$DEVICE_NAME";
};
};
}
100 changes: 100 additions & 0 deletions modules/installer/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2022-2023 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
inputs @ {
config,
lib,
pkgs,
...
}: let
cfg = config.ghaf.installer;
in {
options.ghaf.installer = {
enable = lib.mkEnableOption "installer image";

imgModules = lib.mkOption {
description = lib.mdDoc ''
Modules that will be passed to the installer image.
'';
type = with lib.types; listOf deferredModule;
default = [];
};

# NOTE: These options tries to resemble calamares module system so we'll be
# able to generate calamares installer from same (almost) code base.
# TODO: Add library of bash functions with unified way of asking user
# required information.
installerModules = lib.mkOption {
description = lib.mdDoc ''
Modules describe the information requested from the user
for the installer.
All code must be written for the current pkgs.runtimeShell.
'';
type = with lib.types;
attrsOf (submodule {
options = {
requestCode = lib.mkOption {
description = lib.mdDoc ''
Code that will ask user their preferences.
'';
type = lines;
default = "echo \"Here's should be your installer\"";
};
providedVariables = lib.mkOption {
description = lib.mdDoc ''
Variable that this modules provides.
Used to detect errors with non-existent variables.
'';
type = attrsOf str;
default = {};
};
};
});
};

enabledModules = lib.mkOption {
description = lib.mdDoc ''
Sequence of enabled modules.
'';
type = with lib.types; listOf str;
default = [];
};

installerCode = lib.mkOption {
description = lib.mdDoc ''
Code that will install image based on the information
collected from the installer modules.
All code must be written for the current pkgs.runtimeShell.
'';
type = lib.types.lines;
default = "";
};
};

config = lib.mkIf cfg.enable (let
builtinModulesPaths = map (name: "${./builtin}/${name}.nix") ["flush"];
modulePath2Module = path: import path inputs;
builtinInstallerModules = map modulePath2Module builtinModulesPaths;
in
builtins.foldl' lib.recursiveUpdate {
system.build.installer = let
name2code = name: cfg.installerModules.${name}.requestCode;
enabledModulesCode = map name2code cfg.enabledModules;
enabledModulesCode' = builtins.concatStringsSep "\n" enabledModulesCode;
in
(lib.ghaf.installer {
systemImgCfg = config;
modules = cfg.imgModules;
userCode = ''
# Modules code
${enabledModulesCode'}
# Installer code
${cfg.installerCode}
'';
})
.installerImgDrv;
}
builtinInstallerModules);
}
6 changes: 3 additions & 3 deletions modules/installer/installer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
{
pkgs,
runtimeShell,
systemImgDrv,
userCode ? "",
}:
pkgs.substituteAll {
dir = "bin";
isExecutable = true;

name = "ghaf-installer";
pname = "ghaf-installer";
src = ./installer.sh;
inherit runtimeShell systemImgDrv;
inherit runtimeShell userCode;
}
16 changes: 4 additions & 12 deletions modules/installer/installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ cat <<"EOF"
EOF

echo "Welcome to Ghaf installer!"
echo "To install image choose path to the device on which image will be installed."

lsblk
read -p "Device name [e.g. sda]: " DEVICE_NAME
echo "Starting flushing..."
echo "To install image choose path to the device on which image will be installed."

if sudo dd if=@systemImgDrv@ of=/dev/$DEVICE_NAME conv=sync bs=4K status=progress; then
sync
echo "Flushing finished successfully!"
echo "Now you can detach installation device and reboot to ghaf."
else
echo "Some error occured during flushing process, exit code: $?."
fi
@userCode@

echo "Rebooting..."
sleep 1
shutdown -r
sudo reboot -f
1 change: 1 addition & 0 deletions modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
./development/ssh.nix
./graphics
./hardware/x86_64-linux.nix
./installer
./profiles/applications.nix
./profiles/debug.nix
./profiles/graphics.nix
Expand Down
1 change: 0 additions & 1 deletion targets/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ lib.foldr lib.recursiveUpdate {} [
(import ./generic-x86_64.nix {inherit self lib nixos-generators nixos-hardware microvm;})
(import ./imx8qm-mek.nix {inherit self lib nixos-generators nixos-hardware microvm;})
(import ./microchip-icicle-kit.nix {inherit self lib nixpkgs nixos-hardware;})
(import ./installer.nix {inherit self nixpkgs lib nixos-generators;})
]
21 changes: 21 additions & 0 deletions targets/generic-x86_64.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@
};
}

({config, ...}: {
ghaf.installer = {
enable = true;
imgModules = [
nixos-generators.nixosModules.raw-efi
];
enabledModules = ["flushImage"];
installerCode = ''
echo "Starting flushing..."
if sudo dd if=${config.system.build.${config.formatAttr}}/nixos.img of=/dev/${config.ghaf.installer.installerModules.flushImage.providedVariables.deviceName} conv=sync bs=4K status=progress; then
sync
echo "Flushing finished successfully!"
echo "Now you can detach installation device and reboot to ghaf."
else
echo "Some error occured during flushing process, exit code: $?."
exit
fi
'';
};
})

formatModule

#TODO: how to handle the majority of laptops that need a little
Expand Down
36 changes: 0 additions & 36 deletions targets/installer.nix

This file was deleted.

0 comments on commit 542ccb1

Please sign in to comment.