-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read out flake config if a flake
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
with builtins; let | ||
boolToString = b: | ||
if b | ||
then "true" | ||
else "false"; | ||
|
||
/* | ||
Check whether a value can be coerced to a string. | ||
The value must be a string, path, or attribute set. | ||
String-like values can be used without explicit conversion in | ||
string interpolations and in most functions that expect a string. | ||
*/ | ||
isStringLike = x: | ||
isString x | ||
|| isPath x | ||
|| x ? outPath | ||
|| x ? __toString; | ||
|
||
mapAttrsToList = | ||
# A function, given an attribute's name and value, returns a new value. | ||
f: | ||
# Attribute set to map over. | ||
attrs: | ||
map (name: f name attrs.${name}) (attrNames attrs); | ||
|
||
mkValueString = v: | ||
if v == null | ||
then "" | ||
else if isInt v | ||
then toString v | ||
else if isBool v | ||
then boolToString v | ||
else if isFloat v | ||
then toString v | ||
else if isList v | ||
then concatStringsSep " " v | ||
else if isStringLike v | ||
then v | ||
else ""; | ||
|
||
mkKeyValue = k: v: "${k} = ${mkValueString v}"; | ||
|
||
mkKeyValuePairs = attrs: concatStringsSep "\n" (mapAttrsToList mkKeyValue attrs); | ||
in | ||
mkKeyValuePairs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu | ||
set -o pipefail | ||
|
||
source "${BASH_SOURCE[0]%/*}/vercomp.sh" | ||
if verlte "$NIX_VERSION" "2.13"; then | ||
echo "Do not load flake config on nix version <= 2.13" | ||
exit 0 | ||
fi | ||
|
||
NIX_CONF_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/nix/nix.conf" | ||
|
||
declare nix_conf | ||
tmp="$(mktemp)" | ||
|
||
flake_file=${flake_file:="$tmp"} | ||
flake_url=${flake_url:="github:$OWNER_AND_REPO/$SHA"} | ||
|
||
# only fetch if not (locally) defined (e.g. for testing) | ||
if [ "$flake_file" = "$tmp" ]; then | ||
set -x | ||
gh api \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
"/repos/$OWNER_AND_REPO/contents/flake.nix?ref=$SHA" | | ||
jq -r '.content|gsub("[\n\t]"; "")|@base64d' >"$flake_file" | ||
set +x | ||
fi | ||
|
||
nix_conf="$(mktemp -d)/flake-nixConfig.conf" | ||
NIX_CONFIG=$(nix --experimental-features "nix-command" eval --raw --impure --expr '(import '"$flake_file"').nixConfig or {}' --apply "$(<"${BASH_SOURCE[0]%/*}/nix_config.nix")" | tee "$nix_conf") | ||
|
||
NIX_USER_CONF_FILES="$nix_conf:$NIX_CONF_FILE:${NIX_USER_CONF_FILES:-}" | ||
|
||
echo "NIX_USER_CONF_FILES=$NIX_USER_CONF_FILES" >> $GITHUB_ENV |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# taken from: https://stackoverflow.com/a/4025065 | ||
vercomp () { | ||
if [[ $1 == $2 ]] | ||
then | ||
return 0 | ||
fi | ||
local IFS=. | ||
local i ver1=($1) ver2=($2) | ||
# fill empty fields in ver1 with zeros | ||
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) | ||
do | ||
ver1[i]=0 | ||
done | ||
for ((i=0; i<${#ver1[@]}; i++)) | ||
do | ||
if [[ -z ${ver2[i]} ]] | ||
then | ||
# fill empty fields in ver2 with zeros | ||
ver2[i]=0 | ||
fi | ||
if ((10#${ver1[i]} > 10#${ver2[i]})) | ||
then | ||
return 1 | ||
fi | ||
if ((10#${ver1[i]} < 10#${ver2[i]})) | ||
then | ||
return 2 | ||
fi | ||
done | ||
return 0 | ||
} | ||
|
||
vergt() { | ||
vercomp $1 $2 | ||
case $? in | ||
0) return 1;; | ||
1) return 0;; | ||
2) return 1;; | ||
esac | ||
} | ||
|
||
verlte() { | ||
vercomp $1 $2 | ||
case $? in | ||
0) return 0;; | ||
1) return 1;; | ||
2) return 0;; | ||
esac | ||
} |