Skip to content

Commit 227e5d1

Browse files
committed
Make --network accept a file argument and add a --flake flag
To make it possible to keep multiple network definitions in the same directory, make the `--network` flag accept a file in addition to a directory. Also, add a corresponding `--flake` flag and create short flags for both (`-n` and `-f` respectively).
1 parent eb636fb commit 227e5d1

File tree

3 files changed

+61
-29
lines changed

3 files changed

+61
-29
lines changed

doc/manual/nixops.rst

+16-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ NixOps is a tool for deploying NixOS machines in a network or cloud.
66
Common options
77
==============
88

9+
``--network``; ``-n``
10+
Path to file that contains the NixOps network specification, or the
11+
path to a directory containing it. By default, a file called
12+
``nixops.nix`` in the current working directory is used; if a
13+
directory is specified, it is searched instead.
14+
15+
``--flake``; ``-f``
16+
Path to the flake file that contains the NixOps network
17+
specification in ``outputs.nixopsConfigurations.default``, or the
18+
path to a directory containing it. By default, a file called
19+
``flake.nix`` in the current working directory is used; if a
20+
directory is specified, it is searched instead.
21+
922
``--state``; ``-s``
1023
Path to the state file that contains the deployments. It defaults to
1124
the value of the NIXOPS_STATE environment variable, or
@@ -222,7 +235,6 @@ Synopsis
222235
nixops modify
223236
nixexprs
224237
--name
225-
-n
226238
name
227239
-I
228240
path
@@ -241,9 +253,9 @@ from ``foo`` to ``bar``:
241253

242254
::
243255

244-
$ nixops modify -d foo -n bar expr3.nix expr4.nix
256+
$ nixops modify -d foo --name bar expr3.nix expr4.nix
245257

246-
Note that ``-d`` identifies the existing deployment, while ``-n``
258+
Note that ``-d`` identifies the existing deployment, while ``--name``
247259
specifies its new name.
248260

249261
Command ``nixops clone``
@@ -254,7 +266,6 @@ Synopsis
254266

255267
nixops clone
256268
--name
257-
-n
258269
name
259270
Description
260271
-----------
@@ -273,7 +284,7 @@ To create a new deployment ``bar`` by cloning the deployment ``foo``:
273284

274285
::
275286

276-
$ nixops clone -d foo -n bar
287+
$ nixops clone -d foo --name bar
277288

278289
Command ``nixops delete``
279290
=========================

nixops/args.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@
6161
subparser = add_subparser(subparsers, "create", help="create a new deployment")
6262
subparser.set_defaults(op=op_create)
6363
subparser.add_argument(
64-
"--name", "-n", dest="name", metavar="NAME", help=SUPPRESS
64+
"--name", dest="name", metavar="NAME", help=SUPPRESS
6565
) # obsolete, use -d instead
6666

6767
subparser = add_subparser(subparsers, "modify", help="modify an existing deployment")
6868
subparser.set_defaults(op=op_modify)
6969
subparser.add_argument(
70-
"--name", "-n", dest="name", metavar="NAME", help="new symbolic name of deployment"
70+
"--name", dest="name", metavar="NAME", help="new symbolic name of deployment"
7171
)
7272

7373
subparser = add_subparser(subparsers, "clone", help="clone an existing deployment")
7474
subparser.set_defaults(op=op_clone)
7575
subparser.add_argument(
7676
"--name",
77-
"-n",
7877
dest="name",
7978
metavar="NAME",
8079
help="symbolic name of the cloned deployment",

nixops/script_defs.py

+43-21
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,41 @@
3737

3838

3939
def get_network_file(args: Namespace) -> NetworkFile:
40-
network_dir: str = os.path.abspath(args.network_dir)
41-
42-
if not os.path.exists(network_dir):
43-
raise ValueError("f{network_dir} does not exist")
44-
45-
classic_path = os.path.join(network_dir, "nixops.nix")
46-
flake_path = os.path.join(network_dir, "flake.nix")
47-
48-
classic_exists: bool = os.path.exists(classic_path)
40+
network_path: str = ""
41+
flake_path: str = ""
42+
43+
if args.network_path is not None:
44+
network_path = os.path.abspath(args.network_path)
45+
if os.path.isdir(network_path):
46+
network_path = os.path.join(network_path, "nixops.nix")
47+
if not os.path.exists(network_path):
48+
raise ValueError(f"{network_path} does not exist")
49+
return NetworkFile(network=network_path, is_flake=False)
50+
51+
if args.flake_path is not None:
52+
flake_path = os.path.abspath(args.flake_path)
53+
if os.path.isdir(flake_path):
54+
flake_path = os.path.join(flake_path, "flake.nix")
55+
if not os.path.exists(flake_path):
56+
raise ValueError(f"{flake_path} does not exist")
57+
return NetworkFile(network=flake_path, is_flake=True)
58+
59+
network_path = os.path.join(os.getcwd(), "nixops.nix")
60+
flake_path = os.path.join(os.getcwd(), "flake.nix")
61+
62+
network_exists: bool = os.path.exists(network_path)
4963
flake_exists: bool = os.path.exists(flake_path)
5064

51-
if all((flake_exists, classic_exists)):
52-
raise ValueError("Both flake.nix and nixops.nix cannot coexist")
53-
54-
if classic_exists:
55-
return NetworkFile(network=classic_path, is_flake=False)
65+
if all((flake_exists, network_exists)):
66+
raise ValueError("Both flake.nix and nixops.nix found in current directory")
5667

57-
if flake_exists:
58-
return NetworkFile(network=network_dir, is_flake=True)
68+
if not network_exists and not flake_exists:
69+
raise ValueError("Neither flake.nix nor nixops.nix exists in current directory")
5970

60-
raise ValueError(f"Neither flake.nix nor nixops.nix exists in {network_dir}")
71+
if network_exists:
72+
return NetworkFile(network=network_path, is_flake=False)
73+
else:
74+
return NetworkFile(network=network_path, is_flake=True)
6175

6276

6377
def set_common_depl(depl: nixops.deployment.Deployment, args: Namespace) -> None:
@@ -1154,12 +1168,20 @@ def add_subparser(
11541168
subparsers: _SubParsersAction, name: str, help: str
11551169
) -> ArgumentParser:
11561170
subparser = subparsers.add_parser(name, help=help)
1157-
subparser.add_argument(
1171+
network = subparser.add_mutually_exclusive_group()
1172+
network.add_argument(
11581173
"--network",
1159-
dest="network_dir",
1174+
"-n",
1175+
dest="network_path",
1176+
metavar="FILE",
1177+
help="path to network file or a directory containing nixops.nix",
1178+
)
1179+
network.add_argument(
1180+
"--flake",
1181+
"-f",
1182+
dest="flake_path",
11601183
metavar="FILE",
1161-
default=os.getcwd(),
1162-
help="path to a directory containing either nixops.nix or flake.nix",
1184+
help="path to flake or a directory containing flake.nix",
11631185
)
11641186
subparser.add_argument(
11651187
"--deployment",

0 commit comments

Comments
 (0)