Skip to content

Instantly share code, notes, and snippets.

@ixxie
Created January 4, 2017 10:09
Show Gist options
  • Save ixxie/8f45529be74e0e0bb5b8d9897f4c522d to your computer and use it in GitHub Desktop.
Save ixxie/8f45529be74e0e0bb5b8d9897f4c522d to your computer and use it in GitHub Desktop.
Templating Nix Configurations
#!/usr/bin/env bash
set -e
fluxlib bash
# this builds configuration files from templates
# it takes a template for a nix expression and replaces
# certain variables with values stored in some files.
function maketemp
{
template="$1"
file="$2"
cp "$template" "$file"
com="sed -i"
for var in username fullname hostname timezone
do
#sleep 1
clnvar=${!var//\//\\\/}
com="$com -e 's;\$$var;$clnvar;g'"
done
eval "$com $file"
}
. "$FLUX_HOME/env/user"
. "$FLUX_HOME/env/host"
templates=$(find "$FLUX_HOME/env/nixos/" -name "*.tmp")
for file in $templates
do
maketemp "${file:0:-4}".{tmp,nix}
done
#!/usr/bin/env bash
set -e
function save
{
file="$1"
vars="${@:2}"
declare -p $vars > $file
}
#!/usr/bin/env bash
set -e
fluxlib bash
echo "Enter your user name:"
read username
echo "Enter your full name or alias:"
read fullname
save $FLUX_HOME/env/user username fullname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment