commit cecbf1f326ba6c75ec9ed5221c701ee93a0475a0
parent 233e70a4e64a657cdb95b47af9f3f93dbf7ffb45
Author: Vincent Demeester <vincent@sbr.pm>
Date: Thu, 26 Nov 2020 18:03:31 +0100
Start migrating to bin/ nix-aware for scripts
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Diffstat:
9 files changed, 130 insertions(+), 0 deletions(-)
diff --git a/bin/__dispatch.sh b/bin/__dispatch.sh
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+# This script dispatches invocations transparently to programs instantiated from
+# Nix.
+#
+# To add a new tool, insert it into the case statement below by setting `attr`
+# to the key in nixpkgs which represents the program you want to run.
+set -ueo pipefail
+
+readonly REPO_ROOT=$(dirname "$0")/..
+TARGET_TOOL=$(basename "$0")
+
+case "${TARGET_TOOL}" in
+ univ)
+ attr="tools.univ"
+ ;;
+ system)
+ attr="tools.system"
+ ;;
+ bus)
+ attr="tools.bus"
+ ;;
+ *)
+ echo "The tool '${TARGET_TOOL}' is currently not installed in this repository."
+ exit 1
+ ;;
+esac
+
+result=$(nix-build --no-out-link --attr "${attr}" "${REPO_ROOT}")
+PATH="${result}/bin:$PATH"
+
+exec "${TARGET_TOOL}" "${@}"
diff --git a/bin/bus b/bin/bus
@@ -0,0 +1 @@
+__dispatch.sh+
\ No newline at end of file
diff --git a/bin/system b/bin/system
@@ -0,0 +1 @@
+__dispatch.sh+
\ No newline at end of file
diff --git a/bin/univ b/bin/univ
@@ -0,0 +1 @@
+__dispatch.sh+
\ No newline at end of file
diff --git a/default.nix b/default.nix
@@ -0,0 +1,14 @@
+{ sources ? import ./nix
+, lib ? sources.lib
+, pkgs ? sources.pkgs { }
+, pkgs-unstable ? sources.pkgs-unstable { }
+, nixpkgs ? sources.nixpkgs { }
+}@args:
+let
+ foo = "bar";
+in
+{
+ tools.bus = pkgs.callPackage ./tools/bus { };
+ tools.univ = pkgs.callPackage ./tools/univ { };
+ tools.system = pkgs.callPackage ./tools/system { };
+}
diff --git a/tools/system/default.nix b/tools/system/default.nix
@@ -0,0 +1,11 @@
+{ stdenv }:
+
+stdenv.mkDerivation {
+ name = "system";
+ src = ./.;
+ phases = [ "installPhase" "fixupPhase" ];
+ installPhase = ''
+ mkdir -p $out $out/bin
+ cp $src/system $out/bin/system
+ '';
+}
diff --git a/tools/system/system b/tools/system/system
@@ -0,0 +1,57 @@
+#! /usr/bin/env bash
+set -o pipefail -o noclobber -o nounset
+
+WORK_DIR=${WORK_DIR-}
+if [[ -z "${WORK_DIR}" ]]; then
+ WORK_DIR="$(mktemp --tmpdir -u nix-config-sync.XXXXXXXXXX)"
+ # shellcheck disable=2064
+ trap "rm -rf '$WORK_DIR'" EXIT
+fi
+function error() {
+ local red
+ local reset
+ red="$(tput -Txterm setaf 1)"
+ reset="$(tput -Txterm sgr0)"
+
+ printf "%s%s%s\n" "$red" "$*" "$reset"
+ exit 1
+}
+
+function dry-build() {
+ [ "$#" -eq 0 ] || error "build"
+ local machine
+ machine="$(hostname)"
+ unset NIX_PATH
+ nix build systems.nix --show-trace --dry-run --out-link "$WORK_DIR" "$machine" ||
+ error "Failed to build system"
+}
+
+function build() {
+ [ "$#" -eq 0 ] || error "build"
+ local machine
+ machine="$(hostname)"
+ unset NIX_PATH
+ nix build -f systems.nix --show-trace --out-link "$WORK_DIR" "$machine" ||
+ error "Failed to build system"
+}
+
+function switch() {
+ [ "$#" -eq 0 ] || error "switch"
+ build
+ local switch_bin="$WORK_DIR/bin/switch-to-configuration"
+ sudo nix-env --set \
+ --profile "/nix/var/nix/profiles/system" \
+ "$WORK_DIR" ||
+ error "Failed to activate profile"
+ sudo "$switch_bin" "switch" ||
+ error "Failed to activate system"
+}
+
+function main() {
+ for target in $@; do
+ $target
+ done
+ exit 0
+}
+
+main "$@"
diff --git a/tools/univ/default.nix b/tools/univ/default.nix
@@ -0,0 +1,11 @@
+{ stdenv }:
+
+stdenv.mkDerivation {
+ name = "univ";
+ src = ./.;
+ phases = [ "installPhase" "fixupPhase" ];
+ installPhase = ''
+ mkdir -p $out $out/bin
+ cp $src/univ.sh $out/bin/univ
+ '';
+}
diff --git a/hack/univ.sh b/tools/univ/univ.sh