Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -68,63 +67,48 @@ done

export NIX_CONFIG="experimental-features = flakes nix-command"

nixBin=$(nix eval --impure --raw --expr "
let pkgs = import <nixpkgs> {};
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
sudo nixos-rebuild $action --flake .#"$(cat /etc/hostname)" $trace ${nixBuildArgs[*]} ${globalBuildArgs[*]}
exit 0
fi

nh os $action "$TMP_BUILD_FOLDER" ${nhBuildArgs[*]} -- $trace ${globalBuildArgs[*]}
nh os $action . ${nhBuildArgs[*]} -- $trace ${globalBuildArgs[*]}
59 changes: 59 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
};
}
28 changes: 21 additions & 7 deletions lib/genflake.nix
Original file line number Diff line number Diff line change
@@ -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 <nixpkgs> { inherit system; };
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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}";
}
)
Expand Down
3 changes: 2 additions & 1 deletion modules/options.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
icedosLib,
inputs,
lib,
...
}:
Expand Down Expand Up @@ -40,5 +41,5 @@ in
};
};

config = fromTOML (fileContents ../config.toml);
config = fromTOML (fileContents "${inputs.icedos-config}/config.toml");
}