Commit cecbf1f326ba

Vincent Demeester <vincent@sbr.pm>
2020-11-26 18:03:31
Start migrating to bin/ nix-aware for scripts
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 233e70a
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}" "${@}"
bin/bus
@@ -0,0 +1,1 @@
+__dispatch.sh
\ No newline at end of file
bin/system
@@ -0,0 +1,1 @@
+__dispatch.sh
\ No newline at end of file
bin/univ
@@ -0,0 +1,1 @@
+__dispatch.sh
\ No newline at end of file
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
+  '';
+}
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 "$@"
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
+  '';
+}
hack/univ.sh → tools/univ/univ.sh
File renamed without changes
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 { };
+}