From 83959525655925f86a48670ca9f402f1f2d44d37 Mon Sep 17 00:00:00 2001 From: jim3692 Date: Wed, 26 Nov 2025 14:43:21 +0200 Subject: [PATCH] feat(flake): Add support for using icedos/core as a Flake, instead of needing clone --- build.sh | 54 +++++++++++++++-------------------------- flake.nix | 59 +++++++++++++++++++++++++++++++++++++++++++++ lib/genflake.nix | 28 +++++++++++++++------ modules/options.nix | 3 ++- 4 files changed, 101 insertions(+), 43 deletions(-) create mode 100644 flake.nix diff --git a/build.sh b/build.sh index 8217e8aa..c47949c4 100644 --- a/build.sh +++ b/build.sh @@ -1,5 +1,4 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i bash -p git nh nixfmt-rfc-style rsync +#!/usr/bin/env bash ICEDOS_DIR="/tmp/icedos" CONFIG="$ICEDOS_DIR/configuration-location" @@ -68,58 +67,43 @@ done export NIX_CONFIG="experimental-features = flakes nix-command" -nixBin=$(nix eval --impure --raw --expr " - let pkgs = import {}; - in with builtins; - if (compareVersions \"2.31.0\" pkgs.nix.version) > 0 - then toString (getFlake \"github:NixOS/nixpkgs/nixpkgs-unstable\").legacyPackages.\${pkgs.stdenv.hostPlatform.system}.nix - else toString pkgs.nix -") -export PATH="$nixBin/bin:$PATH" - mkdir -p "$ICEDOS_DIR" +export ICEDOS_BUILD_DIR="$(mktemp -d -t icedos-build-XXXXXXX-0)" +mkdir -p "$ICEDOS_BUILD_DIR" + # Save current directory into a file [ -f "$CONFIG" ] && rm -f "$CONFIG" || sudo rm -rf "$CONFIG" -printf "$PWD" > "$CONFIG" - -# Generate flake.nix -[ -f "$FLAKE" ] && rm -f "$FLAKE" +printf "$ICEDOS_STATE_DIR" > "$CONFIG" export ICEDOS_FLAKE_INPUTS=$(mktemp) [ "$update_repos" == "1" ] && refresh="--refresh" -ICEDOS_UPDATE="$update_repos" ICEDOS_STAGE="genflake" nix eval $refresh $trace --file "./lib/genflake.nix" flakeInputs | nixfmt | sed "1,1d" | sed "\$d" >$ICEDOS_FLAKE_INPUTS -(printf "{ inputs = {" ; cat $ICEDOS_FLAKE_INPUTS ; printf "}; outputs = { ... }: {}; }") >$FLAKE -nix flake prefetch-inputs +ICEDOS_UPDATE="$update_repos" ICEDOS_STAGE="genflake" nix eval $refresh $trace --file "$ICEDOS_ROOT/lib/genflake.nix" flakeInputs | nixfmt | sed "1,1d" | sed "\$d" >$ICEDOS_FLAKE_INPUTS +(printf "{ inputs = {" ; cat $ICEDOS_FLAKE_INPUTS ; printf "}; outputs = { ... }: {}; }") >"$ICEDOS_STATE_DIR/$FLAKE" +( cd "$ICEDOS_STATE_DIR" ; nix flake prefetch-inputs ) -ICEDOS_STAGE="genflake" nix eval $trace --file "./lib/genflake.nix" --raw flakeFinal >$FLAKE -nixfmt "$FLAKE" +ICEDOS_STAGE="genflake" nix eval $trace --file "$ICEDOS_ROOT/lib/genflake.nix" --raw flakeFinal >"$ICEDOS_STATE_DIR/$FLAKE" +nixfmt "$ICEDOS_STATE_DIR/$FLAKE" rm $ICEDOS_FLAKE_INPUTS unset ICEDOS_FLAKE_INPUTS [ "$update" == "1" ] && nix flake update -# Make a tmp folder and build from there -TMP_BUILD_FOLDER="$(mktemp -d -t icedos-build-XXXXXXX-0 | xargs echo)/" - -mkdir -p "$TMP_BUILD_FOLDER" - -rsync -a ./ "$TMP_BUILD_FOLDER" \ ---exclude='.cache' \ ---exclude='.editorconfig' \ +rsync -a "$ICEDOS_CONFIG_ROOT" "$ICEDOS_BUILD_DIR" \ --exclude='.git' \ --exclude='.gitignore' \ ---exclude='.lib' \ ---exclude='.modules' \ ---exclude='.taplo.toml' \ +--exclude='flake.lock' \ +--exclude='flake.nix' \ --exclude='LICENSE' \ ---exclude='README.md' \ ---exclude='build.sh' +--exclude='README.md' + +cp "$ICEDOS_STATE_DIR"/* "$ICEDOS_BUILD_DIR" -echo "Building from path $TMP_BUILD_FOLDER" +echo "Building from path $ICEDOS_BUILD_DIR" +cd $ICEDOS_BUILD_DIR # Build the system configuration if (( ${#nixBuildArgs[@]} != 0 )) || [[ "$isFirstInstall" == 1 ]]; then @@ -127,4 +111,4 @@ if (( ${#nixBuildArgs[@]} != 0 )) || [[ "$isFirstInstall" == 1 ]]; then exit 0 fi -nh os $action "$TMP_BUILD_FOLDER" ${nhBuildArgs[*]} -- $trace ${globalBuildArgs[*]} +nh os $action . ${nhBuildArgs[*]} -- $trace ${globalBuildArgs[*]} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..8dd54f72 --- /dev/null +++ b/flake.nix @@ -0,0 +1,59 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + }; + + outputs = + { + nixpkgs, + self, + ... + }: + { + lib.mkIceDOS = + { configRoot, stateDir ? ".state" }: + let + inherit (builtins) readFile; + inherit (fromTOML (readFile "${configRoot}/config.toml")) icedos; + + system = icedos.system.arch or "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + + inherit (pkgs) + git + lib + nh + nix + nixfmt-rfc-style + rsync + writeShellScript + ; + + inherit (lib) makeBinPath; + + icedosBuild = toString (writeShellScript "icedos-build" '' + set -e + + export PATH="${makeBinPath [ git nh nix nixfmt-rfc-style rsync ]}:$PATH" + export ICEDOS_ROOT="${self}" + export ICEDOS_CONFIG_ROOT="$PWD" + export ICEDOS_STATE_DIR="$PWD/${stateDir}" + mkdir -p "$ICEDOS_STATE_DIR" + + [ -f "$ICEDOS_STATE_DIR/build.sh" ] && rm "$ICEDOS_STATE_DIR/build.sh" + echo "#!/usr/bin/env bash" >>"$ICEDOS_STATE_DIR/build.sh" + echo "set -e" >>"$ICEDOS_STATE_DIR/build.sh" + echo "cd \"$PWD\"" >>"$ICEDOS_STATE_DIR/build.sh" + echo "nix run . -- \"\$@\"" >>"$ICEDOS_STATE_DIR/build.sh" + + bash "${self}/build.sh" "$@" + ''); + in + { + apps.${system}.default = { + type = "app"; + program = icedosBuild; + }; + }; + }; +} diff --git a/lib/genflake.nix b/lib/genflake.nix index 49f3716c..99d17695 100644 --- a/lib/genflake.nix +++ b/lib/genflake.nix @@ -1,6 +1,6 @@ let - inherit (builtins) getEnv readFile; - inherit ((fromTOML (readFile ../config.toml))) icedos; + inherit (builtins) getEnv getFlake readFile; + inherit (fromTOML (readFile "${getEnv "ICEDOS_CONFIG_ROOT"}/config.toml")) icedos; system = icedos.system.arch or "x86_64-linux"; pkgs = import { inherit system; }; @@ -36,6 +36,20 @@ let inherit (c) name; value = { inherit (c) url; }; }) channels) + ++ [ + { + name = "icedos-config"; + value = { + url = "path:${getEnv "ICEDOS_CONFIG_ROOT"}"; + }; + } + { + name = "icedos-core"; + value = { + follows = "icedos-config/icedos"; + }; + } + ] ); nixosModulesText = modulesFromConfig.nixosModulesText; @@ -62,13 +76,13 @@ in inherit (pkgs) lib; inherit (lib) fileContents map; - inherit (builtins) fromTOML; - inherit ((fromTOML (fileContents ./config.toml))) icedos; + inherit (builtins) fromTOML pathExists; + inherit ((fromTOML (fileContents "''${inputs.icedos-config}/config.toml"))) icedos; - icedosLib = import ./lib { + icedosLib = import "''${inputs.icedos-core}/lib" { inherit lib pkgs inputs; config = icedos; - self = ./.; + self = toString inputs.icedos-core; }; inherit (icedosLib) modulesFromConfig; @@ -115,7 +129,7 @@ in ); in { - imports = [ ./modules/options.nix ] ++ getModules ./.extra ++ getModules ./.private; + imports = [ "''${inputs.icedos-core}/modules/options.nix" ] ++ (if (pathExists "''${inputs.icedos-config}/extra-modules") then (getModules "''${inputs.icedos-config}/extra-modules") else []); config.system.stateVersion = "${icedos.system.version}"; } ) diff --git a/modules/options.nix b/modules/options.nix index dc651527..308f0fa7 100644 --- a/modules/options.nix +++ b/modules/options.nix @@ -1,5 +1,6 @@ { icedosLib, + inputs, lib, ... }: @@ -40,5 +41,5 @@ in }; }; - config = fromTOML (fileContents ../config.toml); + config = fromTOML (fileContents "${inputs.icedos-config}/config.toml"); }