Skip to content

Commit

Permalink
refactor: use flake modules for better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Jan 26, 2024
1 parent 0b1dca7 commit c46019d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 99 deletions.
40 changes: 40 additions & 0 deletions flake-modules/config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
lib,
inputs,
self,
pkgs,
system,
lib',
...
}: let
specialArgs = {
inherit inputs self lib';
};
in {
flake = {
systemConfigs.default = inputs.system-manager.lib.makeSystemConfig {
extraSpecialArgs = specialArgs;
modules = [
../modules
../options
{
_module.args.pkgs = lib.mkForce pkgs;
nixpkgs.hostPlatform = system;
}
];
};
nixosConfigurations.default = inputs.nixpkgs.lib.nixosSystem {
inherit system pkgs specialArgs;
modules = [
../modules
({modulesPath, ...}: {
# use virtual machine profile, otherwise file systems need to be defined
imports = [
"${modulesPath}/virtualisation/lxc-container.nix"
];
system.stateVersion = "23.11";
})
];
};
};
}
36 changes: 36 additions & 0 deletions flake-modules/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
inputs,
lib,
lib',
...
}: let
system = "x86_64-linux";
pkgs = import inputs.nixpkgs {
inherit system;
config = {
allowUnfree = true;
cudaSupport = true;
};
overlays = lib.singleton (
final: prev: {
apptainer = prev.apptainer.override {
enableNvidiaContainerCli = false;
};
system-manager = inputs.system-manager.packages.${system}.default;
}
);
};
in {
imports = lib'.flocken.getModules ./.;
systems = lib.singleton system;
_module.args = {
inherit system pkgs;
};
perSystem = {config, ...}: {
_module.args = {
inherit pkgs;
};
packages.default = config.packages.install;
checks = config.packages;
};
}
17 changes: 17 additions & 0 deletions flake-modules/images.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{...}: {
perSystem = {
config,
pkgs,
...
}: {
packages = {
image-base = pkgs.callPackage ../images/base.nix {};
image-jupyter = pkgs.callPackage ../images/jupyter.nix {
base = config.packages.image-base;
};
image-poetry = pkgs.callPackage ../images/poetry.nix {
base = config.packages.image-base;
};
};
};
}
46 changes: 46 additions & 0 deletions flake-modules/scripts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
lib,
self,
...
}: {
perSystem = {pkgs, ...}: {
packages = {
manager = lib.getExe pkgs.system-manager;
install = pkgs.writeShellApplication {
name = "system-manager-rebuild";
text = ''
set -x #echo on
exec ${lib.getExe pkgs.system-manager} "''${1:-switch}" --flake ${self} "''${@:2}"
'';
};
uninstall = pkgs.writeShellApplication {
name = "system-manager-uninstall";
text = ''
set -x #echo on
exec ${lib.getExe pkgs.system-manager} deactivate "''$@"
'';
};
setup = pkgs.writeShellApplication {
name = "system-manager-setup";
text = ''
# only root possible
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
set -x #echo on
# set up nix
cp -f ${../etc/nix.conf} /etc/nix/nix.conf
systemctl restart nix-daemon
# set up cuda support for oci engines like podman
nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml
chmod -R 755 /etc/cdi
# set compute mode to exclusive process (https://stackoverflow.com/a/50056586)
nvidia-smi -c 3
# disable default motd
chmod -x /etc/update-motd.d/*
'';
};
};
};
}
108 changes: 9 additions & 99 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,107 +20,17 @@
self,
nixpkgs,
flake-parts,
system-manager,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} ({
lib,
system,
pkgs,
...
}: {
_module.args = {
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
cudaSupport = true;
};
overlays = lib.singleton (
final: prev: {
apptainer = prev.apptainer.override {
enableNvidiaContainerCli = false;
};
system-manager = inputs.system-manager.packages.${system}.default;
}
);
flake-parts.lib.mkFlake {
inherit inputs;
specialArgs = {
lib' = {
# self = self.lib;
flocken = inputs.flocken.lib;
};
};
systems = lib.singleton system;
perSystem.packages = rec {
default = install;
install = pkgs.writeShellApplication {
name = "system-manager-rebuild";
text = ''
set -x #echo on
exec ${lib.getExe pkgs.system-manager} "''${1:-switch}" --flake ${self} "''${@:2}"
'';
};
uninstall = pkgs.writeShellApplication {
name = "system-manager-uninstall";
text = ''
set -x #echo on
exec ${lib.getExe pkgs.system-manager} deactivate "''$@"
'';
};
setup = pkgs.writeShellApplication {
name = "system-manager-setup";
text = ''
# only root possible
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
set -x #echo on
# set up nix
cp -f ${./etc/nix.conf} /etc/nix/nix.conf
systemctl restart nix-daemon
# set up cuda support for oci engines like podman
nvidia-ctk cdi generate --output=/etc/cdi/nvidia.yaml
chmod -R 755 /etc/cdi
# set compute mode to exclusive process (https://stackoverflow.com/a/50056586)
nvidia-smi -c 3
# disable default motd
chmod -x /etc/update-motd.d/*
'';
};
image-base = pkgs.callPackage ./images/base.nix {};
image-jupyter = pkgs.callPackage ./images/jupyter.nix {base = image-base;};
image-poetry = pkgs.callPackage ./images/poetry.nix {base = image-base;};
};
flake = let
specialArgs = {
inherit inputs self;
lib' = {
flocken = inputs.flocken.lib;
};
};
in {
systemConfigs.default = system-manager.lib.makeSystemConfig {
extraSpecialArgs = specialArgs;
modules = [
./modules
./options
{
_module.args.pkgs = lib.mkForce pkgs;
nixpkgs.hostPlatform = system;
}
];
};
nixosConfigurations.default = nixpkgs.lib.nixosSystem {
inherit system pkgs specialArgs;
modules = [
./modules
({modulesPath, ...}: {
# use virtual machine profile, otherwise file systems need to be defined
imports = [
"${modulesPath}/virtualisation/lxc-container.nix"
];
system.stateVersion = "23.11";
})
];
};
};
});
} {
imports = [./flake-modules];
};
}

0 comments on commit c46019d

Please sign in to comment.