From c46019d8fe7342315a80ef7136b0a402395f48f7 Mon Sep 17 00:00:00 2001 From: Mirko Lenz Date: Fri, 26 Jan 2024 09:23:28 +0100 Subject: [PATCH] refactor: use flake modules for better structure --- flake-modules/config.nix | 40 ++++++++++++++ flake-modules/default.nix | 36 +++++++++++++ flake-modules/images.nix | 17 ++++++ flake-modules/scripts.nix | 46 ++++++++++++++++ flake.nix | 108 ++++---------------------------------- 5 files changed, 148 insertions(+), 99 deletions(-) create mode 100644 flake-modules/config.nix create mode 100644 flake-modules/default.nix create mode 100644 flake-modules/images.nix create mode 100644 flake-modules/scripts.nix diff --git a/flake-modules/config.nix b/flake-modules/config.nix new file mode 100644 index 0000000..179819e --- /dev/null +++ b/flake-modules/config.nix @@ -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"; + }) + ]; + }; + }; +} diff --git a/flake-modules/default.nix b/flake-modules/default.nix new file mode 100644 index 0000000..4c26216 --- /dev/null +++ b/flake-modules/default.nix @@ -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; + }; +} diff --git a/flake-modules/images.nix b/flake-modules/images.nix new file mode 100644 index 0000000..32e4f35 --- /dev/null +++ b/flake-modules/images.nix @@ -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; + }; + }; + }; +} diff --git a/flake-modules/scripts.nix b/flake-modules/scripts.nix new file mode 100644 index 0000000..22ec3ae --- /dev/null +++ b/flake-modules/scripts.nix @@ -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/* + ''; + }; + }; + }; +} diff --git a/flake.nix b/flake.nix index e4720db..99b87c0 100644 --- a/flake.nix +++ b/flake.nix @@ -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]; + }; }