Skip to content

Commit

Permalink
Refactor system config (#55)
Browse files Browse the repository at this point in the history
- Clean up config inputs
- Refactor nixosModules to their correct functionality, apply in hardware and vm config
- Create x86 profile for general host config
- Bring config methods into utils/default.nix
- Clean up redundant/unncessary configs (debugModules,..) because they are included in ghaf

Signed-off-by: Anh Huy Bui <buianhhuy96@gmail.com>
  • Loading branch information
buianhhuy96 authored Aug 30, 2024
1 parent fe06ad9 commit 4f7f8e0
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 111 deletions.
88 changes: 24 additions & 64 deletions config-processor-hardware.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,62 @@
self,
lib,
ghafOS,
nixos-generators,
nixos-hardware,
nixpkgs,
microvm,
}: {
sysconf,
}:
let
updateAttrs = (import ./utils/updateAttrs.nix).updateAttrs;
updateHostConfig = (import ./utils/updateHostConfig.nix).updateHostConfig;
inherit (import ./utils {inherit lib self ghafOS;}) updateAttrs updateHostConfig addCustomLaunchers addSystemPackages importvm;

targetconf = if lib.hasAttr "extend" sysconf
then updateAttrs false (import (lib.path.append ./hardware sysconf.extend) ).sysconf sysconf
else sysconf;

name = targetconf.name;
system = "x86_64-linux";
vms = targetconf.vms;

importvm = vmconf: (import ./modules/virtualization/microvm/vm.nix {inherit ghafOS vmconf self;});
enablevm = vm: {
virtualization.microvm.${vm.name} = {
enable = true;
extraModules = vm.extraModules;
};
};
addSystemPackages = {pkgs, ...}: {environment.systemPackages = map (app: pkgs.${app}) targetconf.systemPackages;};
addCustomLaunchers = { ghaf.graphics.app-launchers.enabled-launchers = targetconf.launchers; };

formatModule = nixos-generators.nixosModules.raw-efi;
target = variant: extraModules: let
target = variant: let
hostConfiguration = lib.nixosSystem {
inherit system;
specialArgs = {inherit lib; inherit ghafOS;};
modules =
[
microvm.nixosModules.host
self.nixosModules.fmo-configs
self.nixosModules.ghaf-common
ghafOS.nixosModules.host

(import "${ghafOS}/modules/microvm/networking.nix")
(import "${ghafOS}/modules/microvm/virtualization/microvm/microvm-host.nix")

# WAR: ghaf mainline has audiovm hardcoded. This causes audiovm defined here
# This should be removed when audiovm on ghaf mainline is fixed.
# JIRA: FMO-43 for monitoring this issue.
(import "${ghafOS}/modules/microvm/virtualization/microvm/audiovm.nix")
{
ghaf = lib.mkMerge (
[
{
hardware.x86_64.common.enable = true;

virtualization.microvm-host.enable = true;
virtualization.microvm-host.networkSupport = true;
host.networking.enable = true;

# Enable all the default UI applications
profiles = {
applications.enable = true;
#TODO clean this up when the microvm is updated to latest
release.enable = variant == "release";
debug.enable = variant == "debug";
};
}
]
++ map (vm: enablevm vms.${vm}) (builtins.attrNames vms)
);
}

addCustomLaunchers
addSystemPackages
formatModule

ghafOS.inputs.nixos-generators.nixosModules.raw-efi
self.nixosModules.fmo-common
self.nixosModules.fmo-host
self.nixosModules.microvm
{
ghaf = {
# Enable all the default UI applications
profiles = {
x86 = {
enable = true;
vms = targetconf.vms;
};
#TODO clean this up when the microvm is updated to latest
release.enable = variant == "release";
debug.enable = variant == "debug";
};
};
boot.kernelParams = [
"intel_iommu=on,igx_off,sm_on"
"iommu=pt"
];
}
]
++ updateHostConfig {inherit lib; inherit targetconf;}
++ map (vm: importvm vms.${vm}) (builtins.attrNames vms)
++ extraModules
++ (addCustomLaunchers targetconf.launchers)
++ (addSystemPackages targetconf.systemPackages)
++ (importvm targetconf.vms)
++ (updateHostConfig targetconf)
++ (if lib.hasAttr "extraModules" targetconf then targetconf.extraModules else []);
};
in {
inherit hostConfiguration;
name = "${name}-${variant}";
package = hostConfiguration.config.system.build.${hostConfiguration.config.formatAttr};
};
debugModules = [{ghaf.development.usb-serial.enable = true;}];
targets = [
(target "debug" debugModules)
(target "release" [])
(target "debug")
(target "release")
];
in {
flake = {
Expand Down
34 changes: 13 additions & 21 deletions config-processor-installers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
self,
lib,
ghafOS,
nixos-generators,
nixos-hardware,
nixpkgs,
microvm,
}: {
sysconf,
}:
let
updateAttrs = (import ./utils/updateAttrs.nix).updateAttrs;
inherit (import ./utils {inherit lib self ghafOS;}) updateAttrs addSystemPackages;

oss = sysconf.oss;
oss_list_name = "installer_os_list";
Expand All @@ -25,25 +21,22 @@ let


installerApp = inst_app: let
installers = (builtins.removeAttrs inst_app ["name"]) //
installers = (builtins.removeAttrs inst_app ["name"]) //
{ oss_path = lib.mkDefault "${oss_list_path}"; };
in installers;
in installers;

addSystemPackages = {pkgs, ...}: {environment.systemPackages = map (app: pkgs.${app}) installerconf.systemPackages;};

formatModule = nixos-generators.nixosModules.iso;
installer = variant: extraModules: let
installer = variant: let
system = "x86_64-linux";

pkgs = import nixpkgs {inherit system;};

installerImgCfg = lib.nixosSystem {
inherit system;
specialArgs = {inherit lib; inherit ghafOS;};
modules =
[
ghafOS.inputs.nixos-generators.nixosModules.iso
self.nixosModules.installer
self.nixosModules.ghaf-common
self.nixosModules.fmo-common

({modulesPath, lib, config, ...}: {
imports = [ (modulesPath + "/profiles/all-hardware.nix") ];
Expand All @@ -52,6 +45,8 @@ let
nixpkgs.config.allowUnfree = true;

hardware.enableAllFirmware = true;

ghaf.development.usb-serial.enable = variant == "debug";

# Installer system profile
# Use less privileged ghaf user
Expand Down Expand Up @@ -90,29 +85,26 @@ let
{
installer.${installerconf.installer.name} = installerApp installerconf.installer;
}

formatModule
addSystemPackages

{
isoImage.squashfsCompression = "lz4";
}
]
++ extraModules
++ (addSystemPackages installerconf.systemPackages)
++ (if lib.hasAttr "extraModules" installerconf then installerconf.extraModules else []);
};
in {
name = "${installerconf.name}-${variant}";
inherit installerImgCfg system;
installerImgDrv = installerImgCfg.config.system.build.${installerImgCfg.config.formatAttr};
};
debugModules = [{ghaf.development.usb-serial.enable = true;}];
targets = [
(installer "debug" debugModules)
(installer "release" [])
(installer "debug")
(installer "release")
];
in {
flake = {
nixosConfigurations =
builtins.listToAttrs (map (t: lib.nameValuePair t.name t.installerImgCfg) targets);
packages = lib.foldr lib.recursiveUpdate {} (map ({name, system, installerImgDrv, ...}: {
${system}.${name} = installerImgDrv;
}) targets);
Expand Down
7 changes: 2 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
# Retrieve inputs from Ghaf
nixpkgs = ghafOS.inputs.nixpkgs;
flake-utils = ghafOS.inputs.flake-utils;
nixos-generators = ghafOS.inputs.nixos-generators;
nixos-hardware = ghafOS.inputs.nixos-hardware;
microvm = ghafOS.inputs.microvm;
flake-parts = ghafOS.inputs.flake-parts;
systems = with flake-utils.lib.system; [
x86_64-linux
Expand All @@ -37,8 +34,8 @@
};
});

generateHwConfig = import ./config-processor-hardware.nix {inherit nixpkgs ghafOS self nixos-hardware nixos-generators lib microvm;};
generateInstConfig = import ./config-processor-installers.nix {inherit nixpkgs ghafOS self nixos-hardware nixos-generators lib microvm;};
generateHwConfig = import ./config-processor-hardware.nix {inherit ghafOS self lib;};
generateInstConfig = import ./config-processor-installers.nix {inherit ghafOS self lib;};
in

flake-parts.lib.mkFlake
Expand Down
30 changes: 19 additions & 11 deletions modules/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@
#
{inputs, ...}: {
flake.nixosModules = {
fmo-configs.imports = [
./packages
./fmo-services
fmo-host.imports = [
inputs.ghafOS.nixosModules.hw-x86_64-generic
inputs.ghafOS.nixosModules.host
inputs.ghafOS.nixosModules.desktop
./profiles/x86.nix
./desktop
../utils/write-to-file
];
installer.imports = [
microvm.imports = [
inputs.ghafOS.inputs.microvm.nixosModules.host
(import "${inputs.ghafOS}/modules/microvm/networking.nix")
(import "${inputs.ghafOS}/modules/microvm/virtualization/microvm/microvm-host.nix")
# WAR: ghaf mainline has audiovm hardcoded. This causes audiovm defined here
# This should be removed when audiovm on ghaf mainline is fixed.
# JIRA: FMO-43 for monitoring this issue.
(import "${inputs.ghafOS}/modules/microvm/virtualization/microvm/audiovm.nix")
];
fmo-common.imports = [
inputs.ghafOS.nixosModules.common
./packages
./installers
./fmo-services
../utils/write-to-file
];
ghaf-common.imports = [
inputs.ghafOS.nixosModules.hw-x86_64-generic
inputs.ghafOS.nixosModules.desktop
inputs.ghafOS.nixosModules.common
installer.imports = [
./installers
];
};
}
}
45 changes: 45 additions & 0 deletions modules/profiles/x86.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
{
config,
lib,
pkgs,
...
}:
let
cfg = config.ghaf.profiles.x86;
in
{
options.ghaf.profiles.x86 = {
enable = lib.mkEnableOption "Enable the basic x86 laptop config";
vms = lib.mkOption {
type = lib.types.attrs;
default = {};
description = ''
Set of VM's configuration.
'';
};
};

config = lib.mkIf cfg.enable {
ghaf = {
hardware.x86_64.common.enable = true;
host.networking.enable = true;

profiles.applications.enable = true;

virtualization= {
microvm-host.enable = true;
microvm-host.networkSupport = true;
}
// builtins.foldl' lib.recursiveUpdate {}
(map (vm: {
microvm.${cfg.vms."${vm}".name} = {
enable = true;
extraModules =
cfg.vms."${vm}".extraModules;
};
}) (builtins.attrNames cfg.vms));
};
};
}
3 changes: 1 addition & 2 deletions modules/virtualization/microvm/vm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@

})
addSystemPackages
self.nixosModules.fmo-configs
self.nixosModules.ghaf-common
self.nixosModules.fmo-common
];
};
cfg = config.ghaf.virtualization.microvm.${vmconf.name};
Expand Down
13 changes: 13 additions & 0 deletions utils/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
{lib, self, ghafOS}: {
updateAttrs = (import ./updateAttrs.nix).updateAttrs;

updateHostConfig = (import ./updateHostConfig.nix {inherit lib;});

addSystemPackages = (packages: [({pkgs, ...}:{environment.systemPackages = map (app: pkgs.${app}) packages;})]);

addCustomLaunchers = (launchers: [{ghaf.graphics.app-launchers.enabled-launchers = launchers;}]);

importvm = (vms: (map (vm: (import ../modules/virtualization/microvm/vm.nix {inherit ghafOS self; vmconf=vms.${vm};}) ) (builtins.attrNames vms)));
}
12 changes: 4 additions & 8 deletions utils/updateHostConfig.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
# SPDX-License-Identifier: Apache-2.0
rec {
updateHostConfig =
{lib, targetconf}:
[
{
{lib,}: (targetconf:
[{
systemd.network = {
networks."10-virbr0" = lib.mkIf (lib.hasAttr "ipaddr" targetconf) {
addresses = [
Expand All @@ -18,6 +15,5 @@ rec {
];
};
};
}
];
}
}]
)

0 comments on commit 4f7f8e0

Please sign in to comment.