aboutsummaryrefslogtreecommitdiff
path: root/pkgs/yo.nix
blob: 8a71e51a9d543832fe82ad6e33e4494fd6bbf8f7 (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
{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
    elseif os.getenv("SUDO") then
      SUDO = os.getenv("SUDO")
    end

    local TARGET = "/home/collin/nixos"

    local function getHostname()
      local f = io.popen("hostname")
      local h = f:read("*l")
      f:close()
      return h
    end

    local function buildConfiguration(args)
      local flake_path = args.flakePath
      local hostname = args.hostname
      local extra_args = table.concat(args.extraArgs or {}, " ")
      local build_cmd = ('nix build %s#nixosConfigurations."%s".config.system.build.toplevel --log-format internal-json %s |& ${pkgs.nix-output-monitor}/bin/nom --json'):format(flake_path, hostname, extra_args)
      print("+ "..build_cmd)
      os.execute(build_cmd)
    end

    local function switchToConfiguration(verb)
      local proc_store_path = io.popen(("readlink -f %s/result"):format(TARGET))
      local store_path = proc_store_path:read("*l")
      proc_store_path:close()

      -- create generation
      local create_gen_cmd = ("nix build --no-link --profile %s %s"):format("/nix/var/nix/profiles/system", store_path)

      local verb_cmd = ("%s/result/bin/switch-to-configuration %s"):format(TARGET, verb)

      local sudo_verb_cmd = ("%s ${pkgs.bashNonInteractive}/bin/bash -c '%s; %s'"):format(SUDO, create_gen_cmd, verb_cmd)
      print("+ "..sudo_verb_cmd)
      os.execute(sudo_verb_cmd)
    end

    local function deployConfiguration(args)
      local flake_path = args.flakePath
      cmd = ("nix run github:serokell/deploy-rs %s -- --skip-checks -- --log-format internal-json |& ${pkgs.nix-output-monitor}/bin/nom --json"):format(flake_path)
      print("+ "..cmd)
      os.execute(cmd)
    end

    if arg[1] == "deploy" or arg[1] == "dep" or arg[1] == "d" then
      deployConfiguration{
        flakePath = TARGET
      }
    elseif arg[1] == "switch" or arg[1] == "sw" then
      buildConfiguration{
        flakePath = TARGET,
        hostname = getHostname()
      }
      switchToConfiguration("switch")
    elseif arg[1] == "boot" then
      buildConfiguration {
        flakePath = TARGET,
        hostname = getHostname()
      }
      switchToConfiguration("boot")
    elseif arg[1] == "test" then
      buildConfiguration {
        flakePath = TARGET,
        hostname = getHostname()
      }
      switchToConfiguration("test")
    elseif arg[1] == "build" then
      local hostname = arg[2] or getHostname()
      buildConfiguration {
        flakePath = TARGET,
        hostname = hostname
      }
    end
  '';
in
  pkgs.writeShellScriptBin "yo" ''
    exec ${pkgs.lua}/bin/lua ${yo} "$@"
  ''