Skip to content

Commit

Permalink
Make --network accept a file argument and add a --flake flag
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
talyz committed Nov 23, 2021
1 parent 35ac020 commit 8b5dd6b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
21 changes: 16 additions & 5 deletions doc/manual/nixops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ NixOps is a tool for deploying NixOS machines in a network or cloud.
Common options
==============

``--network``; ``-n``
Path to file that contains the NixOps network specification, or the
path to a directory containing it. By default, a file called
``nixops.nix`` in the current working directory is used; if a
directory is specified, it is searched instead.

``--flake``; ``-f``
Path to the flake file that contains the NixOps network
specification in ``outputs.nixopsConfigurations.default``, or the
path to a directory containing it. By default, a file called
``flake.nix`` in the current working directory is used; if a
directory is specified, it is searched instead.

``--state``; ``-s``
Path to the state file that contains the deployments. It defaults to
the value of the NIXOPS_STATE environment variable, or
Expand Down Expand Up @@ -222,7 +235,6 @@ Synopsis
nixops modify
nixexprs
--name
-n
name
-I
path
Expand All @@ -241,9 +253,9 @@ from ``foo`` to ``bar``:

::

$ nixops modify -d foo -n bar expr3.nix expr4.nix
$ nixops modify -d foo --name bar expr3.nix expr4.nix

Note that ``-d`` identifies the existing deployment, while ``-n``
Note that ``-d`` identifies the existing deployment, while ``--name``
specifies its new name.

Command ``nixops clone``
Expand All @@ -254,7 +266,6 @@ Synopsis

nixops clone
--name
-n
name
Description
-----------
Expand All @@ -273,7 +284,7 @@ To create a new deployment ``bar`` by cloning the deployment ``foo``:

::

$ nixops clone -d foo -n bar
$ nixops clone -d foo --name bar

Command ``nixops delete``
=========================
Expand Down
5 changes: 2 additions & 3 deletions nixops/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@
subparser = add_subparser(subparsers, "create", help="create a new deployment")
subparser.set_defaults(op=op_create)
subparser.add_argument(
"--name", "-n", dest="name", metavar="NAME", help=SUPPRESS
"--name", dest="name", metavar="NAME", help=SUPPRESS
) # obsolete, use -d instead

subparser = add_subparser(subparsers, "modify", help="modify an existing deployment")
subparser.set_defaults(op=op_modify)
subparser.add_argument(
"--name", "-n", dest="name", metavar="NAME", help="new symbolic name of deployment"
"--name", dest="name", metavar="NAME", help="new symbolic name of deployment"
)

subparser = add_subparser(subparsers, "clone", help="clone an existing deployment")
subparser.set_defaults(op=op_clone)
subparser.add_argument(
"--name",
"-n",
dest="name",
metavar="NAME",
help="symbolic name of the cloned deployment",
Expand Down
64 changes: 43 additions & 21 deletions nixops/script_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,41 @@


def get_network_file(args: Namespace) -> NetworkFile:
network_dir: str = os.path.abspath(args.network_dir)

if not os.path.exists(network_dir):
raise ValueError("f{network_dir} does not exist")

classic_path = os.path.join(network_dir, "nixops.nix")
flake_path = os.path.join(network_dir, "flake.nix")

classic_exists: bool = os.path.exists(classic_path)
network_path: str = ""
flake_path: str = ""

if args.network_path is not None:
network_path = os.path.abspath(args.network_path)
if os.path.isdir(network_path):
network_path = os.path.join(network_path, "nixops.nix")
if not os.path.exists(network_path):
raise ValueError(f"{network_path} does not exist")
return NetworkFile(network=network_path, is_flake=False)

if args.flake_path is not None:
flake_path = os.path.abspath(args.flake_path)
if os.path.isdir(flake_path):
flake_path = os.path.join(flake_path, "flake.nix")
if not os.path.exists(flake_path):
raise ValueError(f"{flake_path} does not exist")
return NetworkFile(network=flake_path, is_flake=True)

network_path = os.path.join(os.getcwd(), "nixops.nix")
flake_path = os.path.join(os.getcwd(), "flake.nix")

network_exists: bool = os.path.exists(network_path)
flake_exists: bool = os.path.exists(flake_path)

if all((flake_exists, classic_exists)):
raise ValueError("Both flake.nix and nixops.nix cannot coexist")

if classic_exists:
return NetworkFile(network=classic_path, is_flake=False)
if all((flake_exists, network_exists)):
raise ValueError("Both flake.nix and nixops.nix found in current directory")

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

raise ValueError(f"Neither flake.nix nor nixops.nix exists in {network_dir}")
if network_exists:
return NetworkFile(network=network_path, is_flake=False)
else:
return NetworkFile(network=network_path, is_flake=True)


def set_common_depl(depl: nixops.deployment.Deployment, args: Namespace) -> None:
Expand Down Expand Up @@ -1127,12 +1141,20 @@ def add_subparser(
subparsers: _SubParsersAction, name: str, help: str
) -> ArgumentParser:
subparser = subparsers.add_parser(name, help=help)
subparser.add_argument(
network = subparser.add_mutually_exclusive_group()
network.add_argument(
"--network",
dest="network_dir",
"-n",
dest="network_path",
metavar="FILE",
help="path to network file or a directory containing nixops.nix",
)
network.add_argument(
"--flake",
"-f",
dest="flake_path",
metavar="FILE",
default=os.getcwd(),
help="path to a directory containing either nixops.nix or flake.nix",
help="path to flake or a directory containing flake.nix",
)
subparser.add_argument(
"--deployment",
Expand Down

0 comments on commit 8b5dd6b

Please sign in to comment.