summary refs log tree commit diff
path: root/pkgs/tools/nix/info
diff options
context:
space:
mode:
authorGraham Christensen <graham@grahamc.com>2017-10-13 16:35:58 -0400
committerGraham Christensen <graham@grahamc.com>2017-10-14 22:01:23 -0400
commitd9d2c4a4a9a103290c61eca3b393c8d1fc054c63 (patch)
treecb87690b4cceba8096da2bc10de8e9907a89753f /pkgs/tools/nix/info
parent534060e608289fdd69b9c01310578a35e36b79e6 (diff)
downloadnixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.tar
nixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.tar.gz
nixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.tar.bz2
nixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.tar.lz
nixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.tar.xz
nixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.tar.zst
nixpkgs-d9d2c4a4a9a103290c61eca3b393c8d1fc054c63.zip
nix-info: init
Diffstat (limited to 'pkgs/tools/nix/info')
-rw-r--r--pkgs/tools/nix/info/default.nix37
-rwxr-xr-xpkgs/tools/nix/info/info.sh169
-rw-r--r--pkgs/tools/nix/info/multiuser.nix12
-rw-r--r--pkgs/tools/nix/info/relaxedsandbox.nix12
-rw-r--r--pkgs/tools/nix/info/sandbox.nix10
5 files changed, 240 insertions, 0 deletions
diff --git a/pkgs/tools/nix/info/default.nix b/pkgs/tools/nix/info/default.nix
new file mode 100644
index 00000000000..756478dccb7
--- /dev/null
+++ b/pkgs/tools/nix/info/default.nix
@@ -0,0 +1,37 @@
+{ stdenv, lib, coreutils, findutils, gnugrep, darwin, shellcheck }:
+stdenv.mkDerivation {
+  name = "nix-info";
+  src = ./info.sh;
+
+  buildInputs = [
+    shellcheck
+  ];
+
+  path = lib.makeBinPath ([
+    coreutils findutils gnugrep
+  ] ++ (if stdenv.isDarwin then [ darwin.DarwinTools ] else []));
+  is_darwin = if stdenv.isDarwin then "yes" else "no";
+
+  sandboxtest = ./sandbox.nix;
+  relaxedsandboxtest = ./relaxedsandbox.nix;
+  multiusertest = ./multiuser.nix;
+
+  unpackCmd = ''
+    mkdir nix-info
+    cp $src ./nix-info/nix-info
+  '';
+
+  buildPhase  = ''
+    substituteAllInPlace ./nix-info
+  '';
+
+  doCheck = true;
+  checkPhase = ''
+    shellcheck ./nix-info
+  '';
+
+  installPhase = ''
+    mkdir -p $out/bin
+    cp ./nix-info $out/bin/nix-info
+  '';
+}
diff --git a/pkgs/tools/nix/info/info.sh b/pkgs/tools/nix/info/info.sh
new file mode 100755
index 00000000000..473e035b8e0
--- /dev/null
+++ b/pkgs/tools/nix/info/info.sh
@@ -0,0 +1,169 @@
+#!/bin/bash
+
+PATH="@path@:$PATH"
+IS_DARWIN="@is_darwin@"
+
+set -eu
+set -o pipefail
+
+DEBUG=0
+MARKDOWN=0
+HOST_OS=0
+SANDBOX=0
+while true; do
+    case "${1:-}" in
+        "")
+            break
+            ;;
+        -d | --debug)
+            set -x
+            DEBUG=1
+            shift
+            ;;
+        -m | --markdown)
+            MARKDOWN=1
+            HOST_OS=1
+            SANDBOX=1
+            shift
+            ;;
+        --host-os)
+            HOST_OS=1
+            shift
+            ;;
+        --sandbox)
+            SANDBOX=1
+            shift
+            ;;
+
+        * )
+            cat <<EOF
+nix-info - get high level info to help with debugging
+
+Options:
+
+ -m, --markdown   formatting for a GitHub issue
+                  implies: --host-os, --sandbox
+
+     --sandbox    include sandbox configuration
+     --host-os    include host OS details
+
+ -h, --help       show this message
+ -d, --debug      debug mode
+
+EOF
+            exit 1
+            ;;
+
+    esac
+done
+
+debuglog() {
+    if [ $DEBUG -eq 1 ]; then
+        cat >&2
+    else
+        cat > /dev/null
+    fi
+}
+
+nixev() {
+    nix-instantiate --eval --strict -E "$1"
+}
+
+desc_system() {
+    nixev '(import <nixpkgs> {}).system'
+}
+
+desc_host_os() {
+    printf "%s" "$(uname -sr)"
+
+    if [ "$IS_DARWIN" = "yes" ]; then
+        printf ", macOS %s" "$(sw_vers -productVersion)"
+    fi
+
+    if [ -f /etc/os-release ]; then
+        (
+            # shellcheck disable=SC1091
+            . /etc/os-release
+            printf ", %s, %s" "${NAME:-$(uname -v)}" "${VERSION:-noversion}"
+        )
+    fi
+}
+
+desc_multi_user() {
+    if nix-build --no-out-link  @multiusertest@ 2>&1 | debuglog; then
+        printf "yes"
+    else
+        printf "no"
+    fi
+}
+
+desc_nixpkgs_path() {
+    nixev '<nixpkgs>'
+}
+
+channel_facts() {
+    find /nix/var/nix/profiles/per-user \
+         -mindepth 2 \
+         -maxdepth 2 \
+         -name channels \
+         -print0 \
+    |\
+    while  IFS= read -r -d '' userchannelset; do
+        manifest="$userchannelset/manifest.nix"
+
+        if [ -e "$manifest" ]; then
+            userchannels=$(nixev \
+                           "builtins.concatStringsSep \", \"
+                             (map (ch: ch.name)
+                               (import \"$manifest\"))")
+
+            fact "channels($(echo "$manifest" | cut -d/ -f7))" \
+                 "$userchannels"
+        fi
+    done
+}
+
+desc_sandbox() {
+    if nix-build --no-out-link @sandboxtest@ 2>&1 | debuglog; then
+        printf "no"
+    elif nix-build --no-out-link @relaxedsandboxtest@ 2>&1 | debuglog; then
+        printf "relaxed"
+    else
+        printf "yes"
+    fi
+}
+
+fact() {
+    name="${1:-0}"
+    value="${2:-0}"
+    last="${3:-1}"
+    if [ $MARKDOWN -eq 0 ]; then
+        printf "%s: %s" "$name" "$value"
+        if [ "$last" -eq 1 ]; then
+            printf ", "
+        fi
+    else
+        printf " - %s: \`%s\`\n" "$name" "$value"
+    fi
+
+    if [ "$last" -eq 0 ]; then
+        echo ""
+    fi
+}
+
+last_fact() {
+    fact "$1" "$2" 0
+}
+
+fact "system" "$(desc_system)"
+if [ $HOST_OS -eq 1 ]; then
+    fact "host os" "$(desc_host_os)"
+fi
+fact "multi-user?" "$(desc_multi_user)"
+if [ $SANDBOX -eq 1 ]; then
+    fact "sandbox" "$(desc_sandbox)"
+fi
+
+fact "version" "$(nix-env --version)"
+channel_facts
+last_fact "nixpkgs" "$(desc_nixpkgs_path)"
diff --git a/pkgs/tools/nix/info/multiuser.nix b/pkgs/tools/nix/info/multiuser.nix
new file mode 100644
index 00000000000..827d5298767
--- /dev/null
+++ b/pkgs/tools/nix/info/multiuser.nix
@@ -0,0 +1,12 @@
+let
+  pkgs = import <nixpkgs> {};
+in pkgs.runCommand "diagnostics-multiuser"
+  {  }
+  ''
+    set -x
+    # no cache: ${toString builtins.currentTime}
+    # For reproducibility, nix always uses nixbld group:
+    # https://github.com/NixOS/nix/blob/1dd29d7aebae706f3e90a18bbfae727f2ed03c70/src/libstore/build.cc#L1896-L1908
+    test "$(groups)" == "nixbld"
+    touch $out
+  ''
diff --git a/pkgs/tools/nix/info/relaxedsandbox.nix b/pkgs/tools/nix/info/relaxedsandbox.nix
new file mode 100644
index 00000000000..625a6ecc39e
--- /dev/null
+++ b/pkgs/tools/nix/info/relaxedsandbox.nix
@@ -0,0 +1,12 @@
+let
+  pkgs = import <nixpkgs> {};
+in pkgs.runCommand "diagnostics-sandbox"
+  {
+    __noChroot = true;
+  }
+  ''
+    set -x
+    # no cache: ${toString builtins.currentTime}
+    test -d "$(dirname "$out")/../var/nix"
+    touch $out
+  ''
diff --git a/pkgs/tools/nix/info/sandbox.nix b/pkgs/tools/nix/info/sandbox.nix
new file mode 100644
index 00000000000..fa4288c2f94
--- /dev/null
+++ b/pkgs/tools/nix/info/sandbox.nix
@@ -0,0 +1,10 @@
+let
+  pkgs = import <nixpkgs> {};
+in pkgs.runCommand "diagnostics-sandbox"
+  { }
+  ''
+    set -x
+    # no cache: ${toString builtins.currentTime}
+    test -d "$(dirname "$out")/../var/nix"
+    touch $out
+  ''