summaryrefslogtreecommitdiff
path: root/packages/nixfiles.nix
blob: 90a58655449c1c92056634c6e937f2eb1124de93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
  git,
  jq,
  lib,
  nix,
  nixfilesSrc ? "$HOME/src/nixfiles",
  openssh,
  writeShellApplication,
}:
writeShellApplication {
  name = "nixfiles";
  runtimeInputs = [git jq nix openssh];
  text = ''
    nixfiles=${lib.escapeShellArg nixfilesSrc}
    cmd="$1"
    shift
    case $cmd in
    update)
      if (( $# )); then
        args=()
        for input do args+=(--update-input "$input"); done
        exec nix flake lock "$nixfiles" "''${args[@]}"
      else
        exec nix flake update -v "$nixfiles"
      fi
      ;;
    rev)
      nix flake metadata --json nixfiles |
        if (( $# )); then
          jq -r --arg input "$1" '.locks.nodes[$input].locked.rev'
        else
          jq -r '.revision // "dirty"'
        fi
      ;;
    repl|eval|build)
      args=()
      for arg do case $arg in
        -w|--wip) args+=(--override-flake config "$nixfiles")
        ;;
        *) args+=("$arg")
      esac done
      set -- "''${args[@]}"
      ;;&
    repl)
      exec nix repl ~/.nix-defexpr "$@"
      ;;
    eval)
      exec nix eval -f ~/.nix-defexpr --json "$@" | jq -r .
      ;;
    build)
      exec nix build -f ~/.nix-defexpr --json "$@" | jq -r .
      ;;
    home)
      attr="nixosConfigurations.$HOSTNAME.config.hm.home.activationPackage"
      export VERBOSE=1
      exec nix shell --verbose "$nixfiles#$attr" "$@" --command home-manager-generation
      ;;
    specialise)
      name=$1
      shift
      exec sudo /run/current-system/specialisation/"$name"/bin/switch-to-configuration switch
      ;;
    revert)
      exec sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch
      ;;
    test-headless)
      exec nixos-rebuild build-vm --verbose --print-build-logs --flake "$nixfiles#test-headless" "$@"
      ;;
    test-headful)
      exec nixos-rebuild build-vm --verbose --print-build-logs --flake "$nixfiles#test-headful" "$@"
      ;;
    @*)
      host="''${cmd#@}"
      hostname="$(ssh -q "$host" 'echo "$HOSTNAME"')"
      exec nixos-rebuild -v --flake "$nixfiles#$hostname" --target-host "$host" --use-remote-sudo "$@"
      ;;
    *)
      exec nixos-rebuild -v --flake "$nixfiles" --use-remote-sudo "$cmd" "$@"
      ;;
    esac
  '';
}