on master help sign in
Add a module.nix configuration to deployments and support non-EC2 targets
commit3b882fb2e5245942a2180a27640dfe1c55991d14 authorElliot Cameron <eacameron@gmail.com> dateSat 15 Feb 2020, 20:30 (7 years ago) parent87186ca
2 files changed+20−8
default.nix+10−6 lib/command/src/Obelisk/Command/Deploy.hs+10−2
default.nix+10−6
@@ -167,10 +167,14 @@ in rec {
167167 '';
168168
169169 serverModules = {
170− mkBaseEc2 = { hostName, routeHost, enableHttps, adminEmail, ... }: {...}: {
170+ mkBaseEc2 = { obeliskPkgs, ... }: {...}: {
171171 imports = [
172− (pkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
172+ (obeliskPkgs.path + /nixos/modules/virtualisation/amazon-image.nix)
173173 ];
174+ ec2.hvm = true;
175+ };
176+
177+ mkDefaultNetworking = { adminEmail, enableHttps, hostName, routeHost, ... }: {...}: {
174178 networking = {
175179 inherit hostName;
176180 firewall.allowedTCPPorts = if enableHttps then [ 80 443 ] else [ 80 ];
@@ -178,7 +182,6 @@ in rec {
178182 security.acme.certs = if enableHttps then {
179183 "${routeHost}".email = adminEmail;
180184 } else {};
181− ec2.hvm = true;
182185 };
183186
184187 mkObeliskApp =
@@ -245,14 +248,15 @@ in rec {
245248 echo ${version} > $out/version
246249 '';
247250
248− server = { exe, hostName, adminEmail, routeHost, enableHttps, version }@args:
251+ server = { exe, hostName, adminEmail, routeHost, enableHttps, version, module ? serverModules.mkBaseEc2 }@args:
249252 let
250253 nixos = import (pkgs.path + /nixos);
251254 in nixos {
252255 system = "x86_64-linux";
253256 configuration = {
254257 imports = [
255− (serverModules.mkBaseEc2 args)
258+ (module (args // { obeliskPkgs = pkgs; }))
259+ (serverModules.mkDefaultNetworking args)
256260 (serverModules.mkObeliskApp args)
257261 ];
258262 };
@@ -379,7 +383,7 @@ in rec {
379383 linuxExeConfigurable = linuxExe;
380384 linuxExe = linuxExe dummyVersion;
381385 exe = serverOn mainProjectOut dummyVersion;
382− server = args@{ hostName, adminEmail, routeHost, enableHttps, version }:
386+ server = args@{ hostName, adminEmail, routeHost, enableHttps, version, module ? serverModules.mkBaseEc2 }:
383387 server (args // { exe = linuxExe version; });
384388 obelisk = import (base' + "/.obelisk/impl") {};
385389 };
lib/command/src/Obelisk/Command/Deploy.hs+10−2
@@ -72,6 +72,12 @@ deployInit thunkPtr deployDir sshKeyPath hostnames route adminEmail enableHttps
7272 --accidentally create a production deployment that uses development
7373 --credentials to connect to some resources. This could result in, e.g.,
7474 --production data backed up to a dev environment.
75+ putLog Notice "Writing default module.nix"
76+ writeDeployConfig deployDir "module.nix"
77+ "{ obeliskPkgs, ... }: {...}: {\n\
78+ \ imports = [(obeliskPkgs.path + /nixos/modules/virtualisation/amazon-image.nix)];\n\
79+ \ ec2.hvm = true;\n\
80+ \}"
7581 withSpinner "Creating project configuration directories" $ do
7682 callProcessAndLogOutput (Notice, Error) $
7783 proc "mkdir" [ "-p"
@@ -113,6 +119,8 @@ deployPush deployPath getNixBuilders = do
113119 Left err -> failWith $ "ob deploy push: couldn't read src thunk: " <> T.pack (show err)
114120 let version = show . _thunkRev_commit $ _thunkPtr_rev thunkPtr
115121 builders <- getNixBuilders
122+ let moduleFile = deployPath </> "module.nix"
123+ moduleFileExists <- liftIO $ doesFileExist moduleFile
116124 buildOutputByHost <- ifor (Map.fromSet (const ()) hosts) $ \host () -> do
117125 --TODO: What does it mean if this returns more or less than 1 line of output?
118126 [result] <- fmap lines $ nixCmd $ NixCmd_Build $ def
@@ -122,13 +130,13 @@ deployPush deployPath getNixBuilders = do
122130 , _target_expr = Nothing
123131 }
124132 & nixBuildConfig_outLink .~ OutLink_None
125− & nixCmdConfig_args .~
133+ & nixCmdConfig_args .~ (
126134 [ strArg "hostName" host
127135 , strArg "adminEmail" adminEmail
128136 , strArg "routeHost" routeHost
129137 , strArg "version" version
130138 , boolArg "enableHttps" enableHttps
131− ]
139+ ] <> [rawArg "module" ("import " <> toNixPath moduleFile) | moduleFileExists ])
132140 & nixCmdConfig_builders .~ builders
133141 pure result
134142 let knownHostsPath = deployPath </> "backend_known_hosts"