aboutsummaryrefslogtreecommitdiff
path: root/pkgs/yo.nix
blob: 11bab17fdc351fbb27f3ee7df40a18933a191a37 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{pkgs, ...}: let
  yo = pkgs.writeText "yo.lua" ''
    local CONF_FILE = os.getenv("HOME") .. "/.config/yo.conf"

    local conf = {}
    for l in io.open(CONF_FILE):lines() do
      if l:match("^(%a+)%s") then
        local k, v = l:match("^(%a+)%s+(.*)$")
        conf[k] = v
      end
    end

    local SUDO = "sudo"
    if conf.sudo then
      SUDO = conf.sudo
    end
    if os.getenv("SUDO") then
      SUDO = os.getenv("SUDO")
    end

    local function collectArgs(start)
      local start = start or 2

      local flags = {}
      if arg[start] ~= nil then
        for i = start, #arg do
          table.insert(flags, arg[i])
        end
      end
      return flags
    end

    local function nixosRebuild(args)
      local sudo = (args.sudo or false) == true
      local verb = args.verb or "switch"
      local flake = args.flake or "/home/collin/nixos"
      local extraArgs = table.concat(args.extraArgs or {}, " ")

      cmd = ("${pkgs.nixos-rebuild-ng}/bin/nixos-rebuild %s --flake %s %s --log-format internal-json |& ${pkgs.nix-output-monitor}/bin/nom --json"):format(verb, flake, extraArgs)
      if sudo then
        local sudo_cmd = ("%s su -c '%s'"):format(SUDO, cmd)
        print("+ "..sudo_cmd)
        os.execute(sudo_cmd)
      else
        print("+ "..cmd)
        os.execute(cmd)
      end
    end

    if arg[1] == "deploy" or arg[1] == "dep" or arg[1] == "d" then
      local deploy_args = {}
      local nix_args = {}

      local found_dashes = false
      if arg[2] ~= nil then
        for i = 2, #arg do
          if arg[i] == "--" then
            found_dashes = true
          elseif found_dashes then
            table.insert(nix_args, arg[i])
          else
            table.insert(deploy_args, arg[i])
          end
        end
      end

      cmd = ("nix run github:serokell/deploy-rs -- --skip-checks %s -- --log-format internal-json %s |& ${pkgs.nix-output-monitor}/bin/nom --json"):format(table.concat(deploy_args, " "), table.concat(nix_args, " "))
      print("+ "..cmd)
      os.execute(cmd)
    elseif arg[1] == "switch" or arg[1] == "sw" or arg[1] == "s" then
      local target = "/home/collin/nixos"
      if arg[2] ~= nil and arg[2]:match("#") then
        target = arg[2]
      end

      nixosRebuild{
        sudo = true,
        verb = "switch",
        flake = target,
        extraArgs = collectArgs(3)
      }
    elseif arg[1] == "boot" or arg[1] == "bo" then
      nixosRebuild{sudo = true, verb = "boot", extraArgs = collectArgs()}
    elseif arg[1] == "test" or arg[1] == "t" then
      nixosRebuild{sudo = true, verb = "test", extraArgs = collectArgs()}
    elseif arg[1] == "build" or arg[1] == "bu" then
      local target = "/home/collin/nixos"
      if arg[2] ~= nil and arg[2]:match("#") then
        target = arg[2]
      end

      nixosRebuild{verb = "build", flake = target, extraArgs = collectArgs(3)}
    end
  '';
in
  pkgs.writeShellScriptBin "yo" ''
    exec ${pkgs.lua}/bin/lua ${yo} "$@"
  ''