Skip to content

Commit

Permalink
feat: read out flake config if a flake
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Jun 30, 2023
1 parent a04e058 commit fa84dc5
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
18 changes: 18 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ inputs:
description: |
Don't use. For bootstrapping purposes only.
load_nixConfig:
default: true
description: |
Reads out a flakes 'nixConfig' attribute, if there is one, and loads
it into the environment config for old-sytle commands to pick it up.
If this cause undesired behaviour, you can switch it off by setting
this to false.
runs:
using: "composite"
Expand All @@ -60,6 +69,15 @@ runs:
NIX_ON_TMPFS: ${{ inputs.nix_on_tmpfs }}
GITHUB_ACCESS_TOKEN: ${{ inputs.github_access_token }}

- name: Preload nixConfig from Flake
if: ${{ inputs.load_nixConfig }}
run: ${{ github.action_path }}/read-nix-config-from-flake.sh
shell: bash
env:
NIX_VERSION: ${{ inputs.nix_version }}
GH_TOKEN: ${{ inputs.github_access_token }} # used by gh cli
OWNER_AND_REPO: ${{ github.repository }}
SHA: ${{ github.sha }}

branding:
icon: zap
Expand Down
11 changes: 11 additions & 0 deletions nix-quick-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
set -eu
set -o pipefail

source "${BASH_SOURCE[0]%/*}/vercomp.sh"

# Figure out system type (TODO don't assume x86_64)
case "$OSTYPE" in
darwin*)
Expand Down Expand Up @@ -73,6 +75,15 @@ if [[ -n "${GITHUB_ACCESS_TOKEN:-}" ]]; then
"access-tokens = github.com=$GITHUB_ACCESS_TOKEN"
fi

# Setup Flakes
if vergt "$NIX_VERSION" "2.13"; then
echo >>"$NIX_CONF_FILE" \
"experimental-features = nix-command flakes"
echo >>"$NIX_CONF_FILE" \
"accept-flake-config = true"
fi


# Populate the nix db
nix="$(readlink /nix/var/nix-quick-install-action/nix)"
"$nix/bin/nix-store" --load-db < /nix/var/nix-quick-install-action/registration
Expand Down
46 changes: 46 additions & 0 deletions nix_config.nix
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
36 changes: 36 additions & 0 deletions read-nix-config-from-flake.sh
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
49 changes: 49 additions & 0 deletions vercomp.sh
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
}

0 comments on commit fa84dc5

Please sign in to comment.