Skip to content

Commit e02c641

Browse files
jim3692IceDBorn
authored andcommitted
feat(flake): Add support for using icedos/core as a Flake, instead of
needing clone
1 parent c06d2a0 commit e02c641

File tree

4 files changed

+102
-44
lines changed

4 files changed

+102
-44
lines changed

build.sh

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#! /usr/bin/env nix-shell
2-
#! nix-shell -i bash -p git nh nixfmt-rfc-style rsync
1+
#!/usr/bin/env bash
32

43
ICEDOS_DIR="/tmp/icedos"
54
CONFIG="$ICEDOS_DIR/configuration-location"
@@ -64,63 +63,48 @@ done
6463

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

67-
nixBin=$(nix eval --impure --raw --expr "
68-
let pkgs = import <nixpkgs> {};
69-
in with builtins;
70-
if (compareVersions \"2.31.0\" pkgs.nix.version) > 0
71-
then toString (getFlake \"github:NixOS/nixpkgs/nixpkgs-unstable\").legacyPackages.\${pkgs.stdenv.hostPlatform.system}.nix
72-
else toString pkgs.nix
73-
")
74-
export PATH="$nixBin/bin:$PATH"
75-
7666
mkdir -p "$ICEDOS_DIR"
7767

68+
export ICEDOS_BUILD_DIR="$(mktemp -d -t icedos-build-XXXXXXX-0)"
69+
mkdir -p "$ICEDOS_BUILD_DIR"
70+
7871
# Save current directory into a file
7972
[ -f "$CONFIG" ] && rm -f "$CONFIG" || sudo rm -rf "$CONFIG"
80-
printf "$PWD" > "$CONFIG"
81-
82-
# Generate flake.nix
83-
[ -f "$FLAKE" ] && rm -f "$FLAKE"
73+
printf "$ICEDOS_STATE_DIR" > "$CONFIG"
8474

8575
export ICEDOS_FLAKE_INPUTS=$(mktemp)
8676

8777
[ "$update_repos" == "1" ] && refresh="--refresh"
8878

89-
ICEDOS_UPDATE="$update_repos" ICEDOS_STAGE="genflake" nix eval $refresh --show-trace --file "./lib/genflake.nix" flakeInputs | nixfmt | sed "1,1d" | sed "\$d" >$ICEDOS_FLAKE_INPUTS
90-
(printf "{ inputs = {" ; cat $ICEDOS_FLAKE_INPUTS ; printf "}; outputs = { ... }: {}; }") >$FLAKE
91-
nix flake prefetch-inputs
79+
ICEDOS_UPDATE="$update_repos" ICEDOS_STAGE="genflake" nix eval $refresh --show-trace --file "$ICEDOS_ROOT/lib/genflake.nix" flakeInputs | nixfmt | sed "1,1d" | sed "\$d" >$ICEDOS_FLAKE_INPUTS
80+
(printf "{ inputs = {" ; cat $ICEDOS_FLAKE_INPUTS ; printf "}; outputs = { ... }: {}; }") >"$ICEDOS_STATE_DIR/$FLAKE"
81+
( cd "$ICEDOS_STATE_DIR" ; nix flake prefetch-inputs )
9282

93-
ICEDOS_STAGE="genflake" nix eval --show-trace --file "./lib/genflake.nix" --raw flakeFinal >$FLAKE
94-
nixfmt "$FLAKE"
83+
ICEDOS_STAGE="genflake" nix eval --show-trace --file "$ICEDOS_ROOT/lib/genflake.nix" --raw flakeFinal >"$ICEDOS_STATE_DIR/$FLAKE"
84+
nixfmt "$ICEDOS_STATE_DIR/$FLAKE"
9585

9686
rm $ICEDOS_FLAKE_INPUTS
9787
unset ICEDOS_FLAKE_INPUTS
9888

9989
[ "$update" == "1" ] && nix flake update
10090

101-
# Make a tmp folder and build from there
102-
TMP_BUILD_FOLDER="$(mktemp -d -t icedos-build-XXXXXXX-0 | xargs echo)/"
103-
104-
mkdir -p "$TMP_BUILD_FOLDER"
105-
106-
rsync -a ./ "$TMP_BUILD_FOLDER" \
107-
--exclude='.cache' \
108-
--exclude='.editorconfig' \
91+
rsync -a "$ICEDOS_CONFIG_ROOT" "$ICEDOS_BUILD_DIR" \
10992
--exclude='.git' \
11093
--exclude='.gitignore' \
111-
--exclude='.lib' \
112-
--exclude='.modules' \
113-
--exclude='.taplo.toml' \
94+
--exclude='flake.lock' \
95+
--exclude='flake.nix' \
11496
--exclude='LICENSE' \
115-
--exclude='README.md' \
116-
--exclude='build.sh'
97+
--exclude='README.md'
98+
99+
cp "$ICEDOS_STATE_DIR"/* "$ICEDOS_BUILD_DIR"
117100

118-
echo "Building from path $TMP_BUILD_FOLDER"
101+
echo "Building from path $ICEDOS_BUILD_DIR"
102+
cd $ICEDOS_BUILD_DIR
119103

120104
# Build the system configuration
121105
if (( ${#nixBuildArgs[@]} != 0 )) || [[ "$isFirstInstall" == 1 ]]; then
122106
sudo nixos-rebuild $action --flake .#"$(cat /etc/hostname)" ${nixBuildArgs[*]} ${globalBuildArgs[*]}
123107
exit 0
124108
fi
125109

126-
nh os $action "$TMP_BUILD_FOLDER" ${nhBuildArgs[*]} -- ${globalBuildArgs[*]}
110+
nh os $action . ${nhBuildArgs[*]} -- ${globalBuildArgs[*]}

flake.nix

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
4+
};
5+
6+
outputs =
7+
{
8+
nixpkgs,
9+
self,
10+
...
11+
}:
12+
{
13+
lib.mkIceDOS =
14+
{ configRoot, stateDir ? ".state" }:
15+
let
16+
inherit (builtins) readFile;
17+
inherit (fromTOML (readFile "${configRoot}/config.toml")) icedos;
18+
19+
system = icedos.system.arch or "x86_64-linux";
20+
pkgs = nixpkgs.legacyPackages.${system};
21+
22+
inherit (pkgs)
23+
git
24+
lib
25+
nh
26+
nix
27+
nixfmt-rfc-style
28+
rsync
29+
writeShellScript
30+
;
31+
32+
inherit (lib) makeBinPath;
33+
34+
icedosBuild = toString (writeShellScript "icedos-build" ''
35+
set -e
36+
37+
export PATH="${makeBinPath [ git nh nix nixfmt-rfc-style rsync ]}:$PATH"
38+
export ICEDOS_ROOT="${self}"
39+
export ICEDOS_CONFIG_ROOT="$PWD"
40+
export ICEDOS_STATE_DIR="$PWD/${stateDir}"
41+
mkdir -p "$ICEDOS_STATE_DIR"
42+
43+
[ -f "$ICEDOS_STATE_DIR/build.sh" ] && rm "$ICEDOS_STATE_DIR/build.sh"
44+
echo "#!/usr/bin/env bash" >>"$ICEDOS_STATE_DIR/build.sh"
45+
echo "set -e" >>"$ICEDOS_STATE_DIR/build.sh"
46+
echo "cd \"$PWD\"" >>"$ICEDOS_STATE_DIR/build.sh"
47+
echo "nix run . -- \"\$@\"" >>"$ICEDOS_STATE_DIR/build.sh"
48+
49+
bash "${self}/build.sh" "$@"
50+
'');
51+
in
52+
{
53+
apps.${system}.default = {
54+
type = "app";
55+
program = icedosBuild;
56+
};
57+
};
58+
};
59+
}

lib/genflake.nix

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let
2-
inherit (builtins) getEnv readFile;
3-
inherit ((fromTOML (readFile ../config.toml))) icedos;
2+
inherit (builtins) getEnv getFlake readFile;
3+
inherit (fromTOML (readFile "${getEnv "ICEDOS_CONFIG_ROOT"}/config.toml")) icedos;
44

55
system = icedos.system.arch or "x86_64-linux";
66
pkgs = import <nixpkgs> { inherit system; };
@@ -36,6 +36,20 @@ let
3636
inherit (c) name;
3737
value = { inherit (c) url; };
3838
}) channels)
39+
++ [
40+
{
41+
name = "icedos-config";
42+
value = {
43+
url = "path:${getEnv "ICEDOS_CONFIG_ROOT"}";
44+
};
45+
}
46+
{
47+
name = "icedos-core";
48+
value = {
49+
follows = "icedos-config/icedos";
50+
};
51+
}
52+
]
3953
);
4054

4155
nixosModulesText = modulesFromConfig.nixosModulesText;
@@ -60,15 +74,15 @@ in
6074
system = "${system}";
6175
pkgs = nixpkgs.legacyPackages.''${system};
6276
inherit (pkgs) lib;
63-
inherit (lib) fileContents flatten map;
77+
inherit (lib) fileContents flatten map optional;
6478
65-
inherit (builtins) fromTOML;
66-
inherit ((fromTOML (fileContents ./config.toml))) icedos;
79+
inherit (builtins) fromTOML pathExists;
80+
inherit ((fromTOML (fileContents "''${inputs.icedos-config}/config.toml"))) icedos;
6781
68-
icedosLib = import ./lib {
82+
icedosLib = import "''${inputs.icedos-core}/lib" {
6983
inherit lib pkgs inputs;
7084
config = icedos;
71-
self = ./.;
85+
self = toString inputs.icedos-core;
7286
};
7387
7488
inherit (icedosLib) modulesFromConfig;
@@ -115,7 +129,7 @@ in
115129
);
116130
in
117131
{
118-
imports = [ ./modules/options.nix ] ++ getModules ./.extra ++ getModules ./.private;
132+
imports = [ "''${inputs.icedos-core}/modules/options.nix" ] ++ (if (pathExists "''${inputs.icedos-config}/extra-modules") then (getModules "''${inputs.icedos-config}/extra-modules") else []);
119133
config.system.stateVersion = "${icedos.system.version}";
120134
}
121135
)

modules/options.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
icedosLib,
3+
inputs,
34
lib,
45
...
56
}:
@@ -40,5 +41,5 @@ in
4041
};
4142
};
4243

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

0 commit comments

Comments
 (0)