Skip to content

Commit 9b3ac91

Browse files
authored
Merge pull request #1264 from grahamc/context-manager-states-rebase
Example NixOps State Backends
2 parents b55cbb2 + 3f7d77a commit 9b3ac91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1865
-1305
lines changed

doc/guides/deploy-without-root.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ the next step.
6161
Configuring the NixOps Network
6262
******************************
6363

64-
Edit your network.nix to specify the machine's
64+
Edit your nixops.nix to specify the machine's
6565
``deployment.targetUser``:
6666

6767
.. code-block:: nix

doc/manual/migrating.rst

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _chap-overview:
2+
3+
Overview
4+
========
5+
6+
This chapter aims to provide guidelines on migrating from NixOps 1.x to 2.0.
7+
8+
.. _sec-layout:
9+
10+
Code layout changes
11+
-------------------
12+
13+
Using NixOps 1.0 multiple deployments spread out over the file and deployed
14+
from any working directory with the ``--deployment (-d)`` parameter.
15+
16+
NixOps 2 however requires a file relative to the invocation working directory.
17+
It needs to be called either ``nixops.nix`` for a traditional deployment or
18+
``flake.nix`` for the as of yet experimental
19+
`flakes support <https://github.com/tweag/rfcs/blob/flakes/rfcs/0049-flakes.md>`.
20+
21+
.. _sec-state-location:
22+
23+
State location
24+
--------------
25+
26+
In NixOps 1.0 deployment state such as provisioned resources are stored in a
27+
SQLite database located in ``~/.nixops``.
28+
29+
NixOps 2 however has pluggable state backends, meaning that you will have to
30+
make a choice where to store this state.
31+
32+
To implement the old behaviour of loading deployment state from the SQLite
33+
database located in ``~/.nixops`` add the following snippet to your deployment:
34+
35+
::
36+
{
37+
network = {
38+
storage.legacy = {};
39+
};
40+
}
41+
42+
To implement a fire-and-forget strategy use this code snippet:
43+
44+
::
45+
{
46+
network = {
47+
storage.memory = {};
48+
};
49+
}
50+
51+
For additional state storage strategies see the various NixOps plugins.
52+
53+
.. _sec-state-migration:
54+
55+
State migration
56+
---------------
57+
58+
Migrating to any non-legacy backend from a previous deployment requires a
59+
manual migration step.
60+
61+
#. Start by configuring the legacy backend as such::
62+
{
63+
network = {
64+
storage.legacy = {};
65+
};
66+
}
67+
68+
#. Then export the current state::
69+
nixops export > state.json
70+
71+
#. Now go ahead and configure your desired state backend.
72+
73+
#. And finally import the old state::
74+
nixops import < state.json
75+
76+
#. Make sure to remove ``state.json`` as it may contain deployment secrets.

flake.nix

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
] ++ (builtins.attrValues linters);
3939

4040
shellHook = ''
41-
export PATH=${builtins.toString ./scripts}:$PATH
41+
git_root=$(${pkgs.git}/bin/git rev-parse --show-toplevel)
42+
export PYTHONPATH=$git_root:$PYTHONPATH
43+
export PATH=$git_root/scripts:$PATH
4244
'';
4345
};
4446

nix/eval-machine-info.nix

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ let
1717
zipAttrs = set: builtins.listToAttrs (
1818
map (name: { inherit name; value = builtins.catAttrs name set; }) (builtins.concatMap builtins.attrNames set));
1919

20+
flakeExpr = (builtins.getFlake flakeUri).outputs.nixopsConfigurations.default;
21+
2022
networks =
2123
let
2224
getNetworkFromExpr = networkExpr:
@@ -31,7 +33,7 @@ let
3133
in
3234
map ({ key }: getNetworkFromExpr key) networkExprClosure
3335
++ optional (flakeUri != null)
34-
((call (builtins.getFlake flakeUri).outputs.nixopsConfigurations.default) // { _file = "<${flakeUri}>"; });
36+
((call flakeExpr) // { _file = "<${flakeUri}>"; });
3537

3638
network = zipAttrs networks;
3739

@@ -260,6 +262,11 @@ in rec {
260262
in
261263
[ f ] ++ map getRequires requires;
262264

265+
exprToArgs = nixopsExpr: f:
266+
if builtins.isFunction nixopsExpr then
267+
map (a: { "${a}" = builtins.toString f; } ) (builtins.attrNames (builtins.functionArgs nixopsExpr))
268+
else [];
269+
263270
fileToArgs = f:
264271
let
265272
nixopsExpr = import f;
@@ -270,6 +277,8 @@ in rec {
270277

271278
getNixOpsArgs = fs: lib.zipAttrs (lib.unique (lib.concatMap fileToArgs (getNixOpsExprs fs)));
272279

273-
nixopsArguments = getNixOpsArgs networkExprs;
280+
nixopsArguments =
281+
if flakeUri == null then getNixOpsArgs networkExprs
282+
else lib.listToAttrs (builtins.map (a: {name = a; value = [ flakeUri ];}) (lib.attrNames (builtins.functionArgs flakeExpr)));
274283

275284
}

nix/templates/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)