-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
executable file
·126 lines (116 loc) · 4.14 KB
/
Main.hs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# LANGUAGE NoImplicitPrelude #-}
import BuildScript
import Data.Map qualified as Map
import Data.Text qualified as T
import Relude
data RebuildSystemMode = Normal | Soft
rebuildSystem :: (MonadIO m) => RebuildSystemMode -> m ()
rebuildSystem rebuildMode = do
logInfo "🔨 begin rebuilding the NixOS + HomeManager configuration from JJBA dotfiles"
logLicense
formatLint
collectGarbage
updateSystem
raiseTmpfs
raiseFD
removePaths
. map Path
$ [ "/etc/nixos/users",
"/etc/nixos/sys",
"/etc/nixos/flake.nix",
"/etc/nixos/flake.lock",
"/etc/nixos/configuration.nix"
]
copyPaths
. Map.fromList
. map (bimap Path Path)
$ [ ("*.nix", "/etc/nixos/"),
("flake.lock", "/etc/nixos/flake.lock"),
("COPYING", "/etc/nixos/"),
("README.org", "/etc/nixos/"),
("users", "/etc/nixos/users"),
("sys", "/etc/nixos/sys")
]
runCommand
(Description "🔨 rebuilding NixOS system + HomeManager flake")
(Command "sudo nixos-rebuild -j1 switch -L --verbose --show-trace --impure --flake '/etc/nixos#nixos'")
runCommand
(Description "🔨 list files in /etc/nixos")
(Command "ls -lAh /etc/nixos")
runCommand
(Description "🔨 send success notification to user")
(Command "notify-send 'Successful rebuilding of NixOS system + Home Manager flake'")
case rebuildMode of
Normal -> do
runCommand
(Description "🔨 restart Emacs daemon")
(Command "pkill emacs || true && pkill emacsclient || true && systemctl --user --no-pager restart emacs")
runCommand
(Description "🔨 check Emacs daemon status")
(Command "systemctl --user --no-pager status emacs")
_ -> pure ()
raiseTmpfs :: (MonadIO m) => m ()
raiseTmpfs = do
runCommand
(Description "🔨 raising the tmpfs size temporarily")
(Command "nix-shell -p mount --run \"sudo mount -o remount,size=50G tmpfs\"")
raiseFD :: (MonadIO m) => m ()
raiseFD = do
runCommand
(Description "🔨 raising the file descriptor size temporarily")
(Command "sudo sysctl -w fs.file-max=100000 && ulimit -n 65535")
collectGarbage :: (MonadIO m) => m ()
collectGarbage = do
maybeLatest <- getLatestUpdateMoment
logInfo ("🔨 latest garbage collection: " <> show maybeLatest)
shouldGarbageCollect <- notSameDay maybeLatest
logInfo ("🔨 shouldGarbageCollect: " <> show shouldGarbageCollect)
when shouldGarbageCollect $ do
runCommand
(Description "🔨 cleaning the Nix store")
(Command "nix-collect-garbage")
addToGarbageCollectedMoments
formatLint :: (MonadIO m) => m ()
formatLint = do
runCommand
(Description "🔨 formatting Nix files in the project")
(Command "find . -name '*.nix' -exec nixfmt {} \\;")
runCommand
(Description "🔨 linting Nix files in the project")
(Command "statix check")
runCommand
(Description "🔨 checking for dead code in Nix files in the project")
(Command "deadnix")
updateSystem :: (MonadIO m) => m ()
updateSystem = do
maybeLatest <- getLatestUpdateMoment
logInfo ("🔨 latest flake update: " <> show maybeLatest)
shouldUpdate <- notSameDay maybeLatest
logInfo ("🔨 shouldUpdate: " <> show shouldUpdate)
when shouldUpdate $ do
logInfo "🔨 begin updating the NixOS + HomeManager configuration from JJBA dotfiles"
logLicense
runCommand
(Description "🔨 updating flake inputs")
(Command "nix flake update")
runCommand
(Description "🔨 fetching archive for flake inputs")
(Command "nix flake archive")
addToUpdateMoments
main :: IO ()
main = do
args <- getArgs
case args of
[task] -> runTimed . runTask . T.pack $ task
_ -> logError "❌ no suitable task has been found!"
runTask :: (MonadIO m) => Text -> m ()
runTask "rebuild-system" = rebuildSystem Normal
runTask "rebuild-system-soft" = rebuildSystem Soft
runTask "update-system" = updateSystem
runTask "format-lint" = formatLint
runTask "collect-garbage" = collectGarbage
runTask _ = logError "❌ no suitable task has been found!"