-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.hs
executable file
·53 lines (44 loc) · 1.46 KB
/
deploy.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
#! /usr/bin/env stack
-- stack --resolver lts-11.8 script --no-nix-pure
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
import Turtle hiding (text)
import NeatInterpolation (text)
-- The path of the NixOS build that we're deploying.
newtype NixOS = NixOS Text
-- The address of the server to which we're deploying.
newtype Server = Server Text
main :: IO ()
main = sh $ do
server <- getServer -- Read the server address from a file
path <- build -- Build NixOS for our server
upload server path -- Upload the build to the server
activate server path -- Start running the new version
getServer :: Shell Server
getServer = do
line <- single (input "server-address.txt")
return (Server (lineToText line))
build :: Shell NixOS
build =
do
line <- single (inproc command args empty)
return (NixOS (lineToText line))
where
command = "nix-build"
args = ["server.nix", "--no-out-link"]
upload :: Server -> NixOS -> Shell ()
upload (Server server) (NixOS path) =
procs command args empty
where
command = "nix-copy-closure"
args = ["--to", "--use-substitutes", server, path]
activate :: Server -> NixOS -> Shell ()
activate (Server server) (NixOS path) =
procs command args empty
where
command = "ssh"
args = [server, remoteCommand]
remoteCommand = [text|
sudo nix-env --profile $profile --set $path
sudo $profile/bin/switch-to-configuration switch
|]
profile = "/nix/var/nix/profiles/system"