├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── default.nix └── src └── main.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: Continuous integration 4 | 5 | jobs: 6 | ci: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | rust: 11 | - stable 12 | - nightly 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - uses: actions-rs/toolchain@v1 17 | with: 18 | profile: minimal 19 | toolchain: ${{ matrix.rust }} 20 | override: true 21 | components: rustfmt, clippy 22 | 23 | - uses: actions-rs/cargo@v1 24 | with: 25 | command: build 26 | 27 | - uses: actions-rs/cargo@v1 28 | with: 29 | command: test 30 | 31 | - uses: actions-rs/cargo@v1 32 | with: 33 | command: fmt 34 | args: --all -- --check 35 | 36 | - uses: actions-rs/cargo@v1 37 | with: 38 | command: clippy 39 | args: -- -D warnings 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "ansi_term" 7 | version = "0.11.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" 10 | dependencies = [ 11 | "winapi", 12 | ] 13 | 14 | [[package]] 15 | name = "atty" 16 | version = "0.2.14" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 19 | dependencies = [ 20 | "hermit-abi", 21 | "libc", 22 | "winapi", 23 | ] 24 | 25 | [[package]] 26 | name = "bitflags" 27 | version = "1.2.1" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 30 | 31 | [[package]] 32 | name = "clap" 33 | version = "2.33.3" 34 | source = "registry+https://github.com/rust-lang/crates.io-index" 35 | checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" 36 | dependencies = [ 37 | "ansi_term", 38 | "atty", 39 | "bitflags", 40 | "strsim", 41 | "textwrap", 42 | "unicode-width", 43 | "vec_map", 44 | ] 45 | 46 | [[package]] 47 | name = "hermit-abi" 48 | version = "0.1.19" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 51 | dependencies = [ 52 | "libc", 53 | ] 54 | 55 | [[package]] 56 | name = "libc" 57 | version = "0.2.98" 58 | source = "registry+https://github.com/rust-lang/crates.io-index" 59 | checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" 60 | 61 | [[package]] 62 | name = "nix-simple-deploy" 63 | version = "0.2.2" 64 | dependencies = [ 65 | "clap", 66 | "subprocess", 67 | ] 68 | 69 | [[package]] 70 | name = "strsim" 71 | version = "0.8.0" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 74 | 75 | [[package]] 76 | name = "subprocess" 77 | version = "0.2.7" 78 | source = "registry+https://github.com/rust-lang/crates.io-index" 79 | checksum = "334b801f9ca2529ba9605109f1d2835f15aff94cd6cfe5afe6ce8cf71e5fd3c4" 80 | dependencies = [ 81 | "libc", 82 | "winapi", 83 | ] 84 | 85 | [[package]] 86 | name = "textwrap" 87 | version = "0.11.0" 88 | source = "registry+https://github.com/rust-lang/crates.io-index" 89 | checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" 90 | dependencies = [ 91 | "unicode-width", 92 | ] 93 | 94 | [[package]] 95 | name = "unicode-width" 96 | version = "0.1.8" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" 99 | 100 | [[package]] 101 | name = "vec_map" 102 | version = "0.8.2" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" 105 | 106 | [[package]] 107 | name = "winapi" 108 | version = "0.3.9" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 111 | dependencies = [ 112 | "winapi-i686-pc-windows-gnu", 113 | "winapi-x86_64-pc-windows-gnu", 114 | ] 115 | 116 | [[package]] 117 | name = "winapi-i686-pc-windows-gnu" 118 | version = "0.4.0" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 121 | 122 | [[package]] 123 | name = "winapi-x86_64-pc-windows-gnu" 124 | version = "0.4.0" 125 | source = "registry+https://github.com/rust-lang/crates.io-index" 126 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 127 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "nix-simple-deploy" 3 | version = "0.2.2" 4 | description = "Deploy software or an entire NixOS system configuration to another NixOS system" 5 | authors = ["misuzu "] 6 | categories = ["command-line-interface", "command-line-utilities"] 7 | keywords = ["nix", "nixos", "devops"] 8 | homepage = "https://github.com/misuzu/nix-simple-deploy" 9 | repository = "https://github.com/misuzu/nix-simple-deploy" 10 | license = "MIT OR Apache-2.0" 11 | readme = "README.md" 12 | edition = "2018" 13 | 14 | [dependencies] 15 | clap = "2.33.3" 16 | subprocess = "0.2.7" 17 | 18 | [profile.release] 19 | codegen-units = 1 20 | lto = true 21 | opt-level = 'z' 22 | panic = 'abort' 23 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | nix-simple-deploy 2 | ================= 3 | ![](https://github.com/misuzu/nix-simple-deploy/workflows/Continuous%20integration/badge.svg) 4 | 5 | ## About 6 | 7 | Deploy a NixOS system configuration with `nix-simple-deploy system ...` to a remote 8 | machine and switch the machine to that system configuration. You can also deploy 9 | a nix store path with `nix-simple-deploy path ...` to a remote machine. 10 | 11 | This is a Rust rewrite of unmaintained [nix-deploy](https://github.com/awakesecurity/nix-deploy). 12 | 13 | ## Usage 14 | 15 | To get started generate signing key first: 16 | ```bash 17 | $ nix-store --generate-binary-cache-key cache.example.com-1 signing-key.sec signing-key.pub 18 | ``` 19 | 20 | Then add contents of ```signing-key.pub``` to remote's ```configuration.nix``` and run ```nixos-rebuild switch```: 21 | ```nix 22 | { 23 | nix.binaryCachePublicKeys = [ "cache.example.com-1:" ]; 24 | } 25 | ``` 26 | 27 | Now you are ready to deploy stuff! 28 | 29 | To simply copy some store path use `nix-simple-deploy path`: 30 | ```bash 31 | $ nix-simple-deploy path \ 32 | --use-remote-sudo \ 33 | --use-substitutes \ 34 | --signing-key signing-key.sec \ 35 | --target-host user@remote-server \ 36 | $(type -p firefox) 37 | ``` 38 | 39 | To deploy whole system use `nix-simple-deploy system`: 40 | ```bash 41 | # copy hardware configuration from remote host to hardware-configuration.nix 42 | $ ssh user@remote-server nixos-generate-config --show-hardware-config > ./hardware-configuration.nix 43 | # build system from configuration.nix 44 | $ nix-build '' -Q -A system -I nixos-config=./configuration.nix 45 | # deploy system to remote host 46 | $ nix-simple-deploy system \ 47 | --use-remote-sudo \ 48 | --use-substitutes \ 49 | --signing-key signing-key.sec \ 50 | --target-host user@remote-server \ 51 | $(readlink -f ./result) \ 52 | switch 53 | ``` 54 | 55 | ## Install 56 | 57 | Just add it to `environment.systemPackages` (nixpkgs-unstable): 58 | ```nix 59 | { 60 | environment.systemPackages = [ 61 | pkgs.nix-simple-deploy 62 | ]; 63 | } 64 | ``` 65 | 66 | To run ```nix-simple-deploy``` from git tree run: 67 | ```bash 68 | $ nix-shell -p cargo -p nix-serve 69 | $ cargo run -- --help 70 | ``` 71 | 72 | You can also build `nix-simple-deploy` directly from provided `default.nix` expression from this repo. Just setup `rev` value and appropriate `sha256`: 73 | 74 | ```nix 75 | { 76 | nix-simple-deploy = pkgs.callPackage (pkgs.fetchFromGitHub { 77 | rev = "..."; 78 | owner = "misuzu"; 79 | repo = "nix-simple-deploy"; 80 | sha256 = "..."; 81 | }) {}; 82 | } 83 | ``` 84 | Then you can add it to your `shell.nix` `buildInputs` or system wide into `environment.systemPackages`: 85 | 86 | ```nix 87 | { 88 | environment.systemPackages = [ 89 | nix-simple-deploy 90 | ]; 91 | } 92 | ``` 93 | 94 | ## Help output 95 | 96 | ```bash 97 | $ nix-simple-deploy --help 98 | Deploy software or an entire NixOS system configuration to another NixOS system 99 | 100 | USAGE: 101 | nix-simple-deploy 102 | 103 | FLAGS: 104 | -h, --help Prints help information 105 | -V, --version Prints version information 106 | 107 | SUBCOMMANDS: 108 | help Prints this message or the help of the given subcommand(s) 109 | path Deploy a path to the NixOS target host 110 | system Deploy a system to the NixOS target host 111 | ``` 112 | 113 | ```bash 114 | $ nix-simple-deploy path --help 115 | Deploy a path to the NixOS target host 116 | 117 | USAGE: 118 | nix-simple-deploy path [FLAGS] [OPTIONS] --target-host 119 | 120 | FLAGS: 121 | -h, --help Prints help information 122 | --use-remote-sudo When set, nix-simple-deploy prefixes remote commands that run on the --target-host systems 123 | with sudo. Setting this option allows deploying using remote non-root user 124 | -s, --use-substitutes Attempt to download missing paths on the target machine using Nix’s substitute mechanism. 125 | Any paths that cannot be substituted on the target are still copied normally from the 126 | source 127 | 128 | OPTIONS: 129 | --extra-ssh-options Extra options for ssh binary 130 | -n, --nix-serve-port 131 | Port used for nix-serve, use this option if you have other services that use port 9999 on local or remote 132 | machine [default: 9999] 133 | -p, --profile-path Profile path 134 | -k, --signing-key File containing the secret signing key 135 | --store Use different nix store root 136 | -t, --target-host Specifies the NixOS target host 137 | 138 | ARGS: 139 | Nix store path 140 | ``` 141 | 142 | ```bash 143 | $ nix-simple-deploy system --help 144 | Deploy a system to the NixOS target host 145 | 146 | USAGE: 147 | nix-simple-deploy system [FLAGS] [OPTIONS] --target-host 148 | 149 | FLAGS: 150 | -h, --help Prints help information 151 | --use-remote-sudo When set, nix-simple-deploy prefixes remote commands that run on the --target-host systems 152 | with sudo. Setting this option allows deploying using remote non-root user 153 | -s, --use-substitutes Attempt to download missing paths on the target machine using Nix’s substitute mechanism. 154 | Any paths that cannot be substituted on the target are still copied normally from the 155 | source 156 | 157 | OPTIONS: 158 | --extra-ssh-options Extra options for ssh binary 159 | -n, --nix-serve-port 160 | Port used for nix-serve, use this option if you have other services that use port 9999 on local or remote 161 | machine [default: 9999] 162 | -p, --profile-path Profile path [default: /nix/var/nix/profiles/system] 163 | -k, --signing-key File containing the secret signing key 164 | -t, --target-host Specifies the NixOS target host 165 | 166 | ARGS: 167 | Nix store path 168 | Desired operation [possible values: switch, boot, test, dry-activate, reboot] 169 | ``` 170 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | # copy this file to directory with your configuration.nix and add this to configuration.nix 2 | # environment.systemPackages = [ 3 | # (pkgs.callPackage ./nix-simple-deploy.nix { }) 4 | # ]; 5 | { lib, fetchFromGitHub, rustPlatform, makeWrapper, openssh, nix-serve }: 6 | rustPlatform.buildRustPackage rec { 7 | pname = "nix-simple-deploy"; 8 | version = "0.2.0"; 9 | 10 | src = ./.; 11 | 12 | cargoSha256 = "1n6q962lbrlmj7p2i290jww65a0s9xckf1pnqyn43fpx4a9ibqaa"; 13 | 14 | nativeBuildInputs = [ makeWrapper ]; 15 | 16 | postInstall = '' 17 | wrapProgram "$out/bin/nix-simple-deploy" \ 18 | --prefix PATH : "${lib.makeBinPath [ openssh nix-serve ]}" 19 | ''; 20 | 21 | meta = with lib; { 22 | description = "Deploy software or an entire NixOS system configuration to another NixOS system"; 23 | homepage = "https://github.com/misuzu/nix-simple-deploy"; 24 | license = with licenses; [ asl20 /* OR */ mit ]; 25 | maintainers = with maintainers; [ misuzu ]; 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use clap::*; 2 | use subprocess::{Exec, NullFile, Result}; 3 | 4 | use std::fs; 5 | use std::process::exit; 6 | use std::time; 7 | 8 | fn get_ssh_tool( 9 | target_host: &str, 10 | extra_ssh_options: Option<&str>, 11 | nix_serve_port: u16, 12 | use_remote_sudo: bool, 13 | ) -> String { 14 | let cmd = format!( 15 | "ssh {} -R {}:127.0.0.1:{} {}", 16 | extra_ssh_options.unwrap_or(""), 17 | nix_serve_port, 18 | nix_serve_port, 19 | target_host 20 | ); 21 | if use_remote_sudo { 22 | format!("{} sudo", cmd) 23 | } else { 24 | cmd 25 | } 26 | } 27 | 28 | fn deploy_path( 29 | ssh_tool: &str, 30 | nix_serve_port: u16, 31 | use_substitutes: bool, 32 | path: &str, 33 | signing_key: Option<&str>, 34 | store: Option<&str>, 35 | profile_path: Option<&str>, 36 | ) -> Result<()> { 37 | let mut cmd = Exec::cmd("nix-serve") 38 | .arg("-p") 39 | .arg(nix_serve_port.to_string()) 40 | .stdout(NullFile) 41 | .stderr(NullFile); 42 | if let Some(key_path) = signing_key { 43 | cmd = cmd.env("NIX_SECRET_KEY_FILE", key_path); 44 | }; 45 | match cmd.popen() { 46 | Ok(ref mut nix_serve) => { 47 | if let Some(exit_status) = nix_serve.wait_timeout(time::Duration::from_secs(1))? { 48 | println!("nix-serve exited too early: {:?}", exit_status); 49 | exit(1); 50 | }; 51 | 52 | let path = fs::read_link(path) 53 | .unwrap_or_else(|_| path.into()) 54 | .as_path() 55 | .display() 56 | .to_string(); 57 | 58 | let cmd = if let Some(profile_path) = profile_path { 59 | format!( 60 | "{} nix-env --option {}substituters http://127.0.0.1:{} {} {} -p {} --set {}", 61 | ssh_tool, 62 | (if use_substitutes { "extra-" } else { "" }), 63 | nix_serve_port, 64 | signing_key.map_or("--option require-sigs false", |_| ""), 65 | store.map_or("".to_string(), |s| format!("--store {}", s)), 66 | profile_path, 67 | path 68 | ) 69 | } else { 70 | format!( 71 | "{} nix build --option {}substituters http://127.0.0.1:{} {} {} --print-missing -v --no-link {}", 72 | ssh_tool, 73 | (if use_substitutes { "extra-" } else { "" }), 74 | nix_serve_port, 75 | signing_key.map_or("--option require-sigs false", |_| ""), 76 | store.map_or("".to_string(), |s| format!("--store {}", s)), 77 | path 78 | ) 79 | }; 80 | 81 | let exit_status = Exec::shell(cmd).join()?; 82 | 83 | nix_serve.terminate()?; 84 | 85 | if !exit_status.success() { 86 | exit(1); 87 | } 88 | 89 | Ok(()) 90 | } 91 | Err(e) => { 92 | println!("Error while starting nix-serve:"); 93 | Err(e) 94 | } 95 | } 96 | } 97 | 98 | fn deploy_system( 99 | ssh_tool: &str, 100 | nix_serve_port: u16, 101 | use_substitutes: bool, 102 | path: &str, 103 | signing_key: Option<&str>, 104 | action: &str, 105 | profile_path: &str, 106 | ) -> Result<()> { 107 | let remote_action = if action == "reboot" { "boot" } else { action }; 108 | 109 | let profile_path = match remote_action { 110 | "switch" | "boot" => Some(profile_path), 111 | _ => None, 112 | }; 113 | 114 | deploy_path( 115 | ssh_tool, 116 | nix_serve_port, 117 | use_substitutes, 118 | path, 119 | signing_key, 120 | None, 121 | profile_path.as_deref(), 122 | )?; 123 | 124 | let cmd = format!( 125 | "{} {}/bin/switch-to-configuration {}", 126 | ssh_tool, 127 | profile_path.as_deref().unwrap_or(path), 128 | remote_action 129 | ); 130 | 131 | let exit_status = Exec::shell(cmd).join()?; 132 | 133 | if !exit_status.success() { 134 | exit(1); 135 | } 136 | 137 | if action == "reboot" { 138 | let mut p = Exec::shell(format!("{} reboot", ssh_tool)) 139 | .detached() 140 | .popen()?; 141 | let _ = p.wait_timeout(time::Duration::from_secs(10)); 142 | } 143 | 144 | Ok(()) 145 | } 146 | 147 | fn main() { 148 | let matches = App::new(crate_name!()) 149 | .version(crate_version!()) 150 | .about(crate_description!()) 151 | .setting(AppSettings::SubcommandRequiredElseHelp) 152 | .setting(AppSettings::VersionlessSubcommands) 153 | .subcommand( 154 | SubCommand::with_name("path") 155 | .about("Deploy a path to the NixOS target host") 156 | .arg( 157 | Arg::with_name("target-host") 158 | .short("t") 159 | .long("target-host") 160 | .help("Specifies the NixOS target host") 161 | .value_name("USER@HOST") 162 | .required(true), 163 | ) 164 | .arg( 165 | Arg::with_name("extra-ssh-options") 166 | .long("extra-ssh-options") 167 | .help("Extra options for ssh binary") 168 | .value_name("ssh-options"), 169 | ) 170 | .arg( 171 | Arg::with_name("nix-serve-port") 172 | .short("n") 173 | .long("nix-serve-port") 174 | .help( 175 | "Port used for nix-serve, use this option \ 176 | if you have other services that use port 9999 \ 177 | on local or remote machine", 178 | ) 179 | .value_name("port") 180 | .default_value("9999"), 181 | ) 182 | .arg( 183 | Arg::with_name("signing-key") 184 | .short("k") 185 | .long("signing-key") 186 | .help("File containing the secret signing key") 187 | .value_name("/path/to/signing-key"), 188 | ) 189 | .arg( 190 | Arg::with_name("store") 191 | .long("store") 192 | .help("Use different nix store root") 193 | .value_name("/mnt"), 194 | ) 195 | .arg( 196 | Arg::with_name("use-substitutes") 197 | .short("s") 198 | .long("use-substitutes") 199 | .help( 200 | "Attempt to download missing paths on the target \ 201 | machine using Nix’s substitute mechanism. \ 202 | Any paths that cannot be substituted on the \ 203 | target are still copied normally from the source", 204 | ), 205 | ) 206 | .arg( 207 | Arg::with_name("use-remote-sudo") 208 | .long("use-remote-sudo") 209 | .help( 210 | "When set, nix-simple-deploy prefixes remote commands \ 211 | that run on the --target-host systems with sudo. \ 212 | Setting this option allows deploying using remote non-root user", 213 | ), 214 | ) 215 | .arg( 216 | Arg::with_name("profile-path") 217 | .short("p") 218 | .long("profile-path") 219 | .help("Profile path") 220 | .value_name("/path/to/nix/profile"), 221 | ) 222 | .arg(Arg::with_name("PATH").help("Nix store path").required(true)), 223 | ) 224 | .subcommand( 225 | SubCommand::with_name("system") 226 | .about("Deploy a system to the NixOS target host") 227 | .arg( 228 | Arg::with_name("target-host") 229 | .short("t") 230 | .long("target-host") 231 | .help("Specifies the NixOS target host") 232 | .value_name("USER@HOST") 233 | .required(true), 234 | ) 235 | .arg( 236 | Arg::with_name("extra-ssh-options") 237 | .long("extra-ssh-options") 238 | .help("Extra options for ssh binary") 239 | .value_name("ssh-options"), 240 | ) 241 | .arg( 242 | Arg::with_name("nix-serve-port") 243 | .short("n") 244 | .long("nix-serve-port") 245 | .help( 246 | "Port used for nix-serve, use this option \ 247 | if you have other services that use port 9999 \ 248 | on local or remote machine", 249 | ) 250 | .value_name("port") 251 | .default_value("9999"), 252 | ) 253 | .arg( 254 | Arg::with_name("signing-key") 255 | .short("k") 256 | .long("signing-key") 257 | .help("File containing the secret signing key") 258 | .value_name("/path/to/signing-key"), 259 | ) 260 | .arg( 261 | Arg::with_name("use-substitutes") 262 | .short("s") 263 | .long("use-substitutes") 264 | .help( 265 | "Attempt to download missing paths on the target \ 266 | machine using Nix’s substitute mechanism. \ 267 | Any paths that cannot be substituted on the \ 268 | target are still copied normally from the source", 269 | ), 270 | ) 271 | .arg( 272 | Arg::with_name("use-remote-sudo") 273 | .long("use-remote-sudo") 274 | .help( 275 | "When set, nix-simple-deploy prefixes remote commands \ 276 | that run on the --target-host systems with sudo. \ 277 | Setting this option allows deploying using remote non-root user", 278 | ), 279 | ) 280 | .arg( 281 | Arg::with_name("profile-path") 282 | .short("p") 283 | .long("profile-path") 284 | .help("Profile path") 285 | .value_name("/path/to/nix/profile") 286 | .default_value("/nix/var/nix/profiles/system"), 287 | ) 288 | .arg(Arg::with_name("PATH").help("Nix store path").required(true)) 289 | .arg( 290 | Arg::with_name("ACTION") 291 | .help("Desired operation") 292 | .possible_values(&["switch", "boot", "test", "dry-activate", "reboot"]) 293 | .required(true), 294 | ), 295 | ) 296 | .get_matches(); 297 | 298 | let result = match matches.subcommand() { 299 | ("path", Some(path_matches)) => deploy_path( 300 | &get_ssh_tool( 301 | path_matches.value_of("target-host").unwrap(), 302 | path_matches.value_of("extra-ssh-options"), 303 | path_matches 304 | .value_of("nix-serve-port") 305 | .unwrap() 306 | .parse() 307 | .unwrap(), 308 | path_matches.is_present("use-remote-sudo"), 309 | ), 310 | path_matches 311 | .value_of("nix-serve-port") 312 | .unwrap() 313 | .parse() 314 | .unwrap(), 315 | path_matches.is_present("use-substitutes"), 316 | path_matches.value_of("PATH").unwrap(), 317 | path_matches.value_of("signing-key"), 318 | path_matches.value_of("store"), 319 | path_matches.value_of("profile-path"), 320 | ), 321 | ("system", Some(system_matches)) => deploy_system( 322 | &get_ssh_tool( 323 | system_matches.value_of("target-host").unwrap(), 324 | system_matches.value_of("extra-ssh-options"), 325 | system_matches 326 | .value_of("nix-serve-port") 327 | .unwrap() 328 | .parse() 329 | .unwrap(), 330 | system_matches.is_present("use-remote-sudo"), 331 | ), 332 | system_matches 333 | .value_of("nix-serve-port") 334 | .unwrap() 335 | .parse() 336 | .unwrap(), 337 | system_matches.is_present("use-substitutes"), 338 | system_matches.value_of("PATH").unwrap(), 339 | system_matches.value_of("signing-key"), 340 | system_matches.value_of("ACTION").unwrap(), 341 | system_matches.value_of("profile-path").unwrap(), 342 | ), 343 | _ => unreachable!(), 344 | }; 345 | if let Err(e) = result { 346 | println!("{}", e); 347 | exit(1); 348 | } 349 | } 350 | --------------------------------------------------------------------------------