Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apps): add git-push and deploy scripts for darwin and nixos #106

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,29 @@ After making changes to the configuration files of a host, a flake app must be e
```command
nix flake show
```

notice apps prefixed with `deploy-`
notice apps prefixed with `ssh-`, `git-push-`, and `deploy-`.

### Deploy changes to host

Prerequisites:
* all relevant changes are committed to the current branch.
* `git push` access to holochain/holochain-infra
* authorized key for the `deployUser` on the remote host

The first command will push the current git HEAD to the `origin` git remote at a branch specific to the hostname.
The second command will cause a `nixos-rebuild switch ...` on the host from its branch.

```command
nix run .\#git-push-{hostname}
nix run .#deploy-{hostname}
```

### Deploy to all hosts
These scripts also have arguments for rudimentary customization.
Here, it pushes to the git remote called `upstream`, and then runs a `build` (instead of a `switch`) on the remote host:

```
nix run .\#deploy-linux-builder-01
nix run .\#deploy-dweb-reverse-tls-proxy
nix run .\#deploy-macos-01
nix run .\#deploy-macos-02
nix run .\#deploy-macos-03
nix run .\#deploy-macos-04
nix run .\#git-push-sbd-0_main_infra_holo_host upstream
nix run .\#deploy-sbd-0_main_infra_holo_host build
```

### Update dependencies (nixpkgs version)
Expand Down
10 changes: 5 additions & 5 deletions modules/flake-parts/apps.deploy-/darwin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
])}:$PATH"
set -x

rsync -r --delete ${self}/ ${deployUser}@${hostName}:/private/tmp/deploy-flake
flake_base=github:holochain/holochain-infra/deploy/${attrName}

ssh ${deployUser}@${hostName} /nix/var/nix/profiles/default/bin/nix \
--extra-experimental-features '"flakes nix-command"' \
build \
build --refresh \
-o /private/tmp/next-system \
/private/tmp/deploy-flake#darwinConfigurations.'"${attrName}"'.system
$flake_base#darwinConfigurations.'"${attrName}"'.system

ssh ${deployUser}@${hostName} /private/tmp/next-system/sw/bin/darwin-rebuild \
-j4 \
"''${1:-switch}" --flake /private/tmp/deploy-flake#'"${attrName}"'
-j4 --refresh \
"''${1:-switch}" --flake $flake_base#'"${attrName}"'
'';

mkDarwinDeployApp = attrName: config:
Expand Down
8 changes: 3 additions & 5 deletions modules/flake-parts/apps.deploy-/linux.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
pkgs.writeShellScript "deploy-${hostName}" ''
set -Eeo pipefail
export PATH="${lib.makeBinPath (with pkgs; [
nix
rsync
git
coreutils
])}:$PATH"
set -x

rsync -r --delete ${self}/ root@${hostName}:/tmp/deploy-flake

ssh root@${hostName} nixos-rebuild \
-j4 \
"''${1:-switch}" --flake /tmp/deploy-flake#'"${attrName}"'
"''${1:-switch}" --flake github:holochain/holochain-infra/deploy/${attrName}#'"${attrName}"'
'';

mkLinuxDeployApp = attrName: config:
Expand Down
37 changes: 37 additions & 0 deletions modules/flake-parts/apps.git-push-/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
self,
lib,
...
}: {
perSystem = {pkgs, ...}: let
mkGitPush = {
attrName,
hostName,
}:
pkgs.writeShellScript "git-push-${hostName}" ''
set -Eeou pipefail
export PATH="${lib.makeBinPath (with pkgs; [
git
])}:$PATH"
set -x

remote=''${1:-origin}
local_branch=''${2:-HEAD}
shift || :
shift || :

git push $remote $local_branch:refs/heads/deploy/${attrName} ''${@}
'';

mkGitPushApp = attrName: config:
lib.nameValuePair "git-push-${attrName}" {
type = "app";
program = builtins.toString (mkGitPush {
inherit attrName;
inherit (config.config) hostName;
});
};
in {
config.apps = lib.mapAttrs' mkGitPushApp (self.darwinConfigurations // self.nixosConfigurations);
};
}
2 changes: 2 additions & 0 deletions modules/flake-parts/darwinConfigurations.macos-01/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
self,
lib,
inputs,
...
Expand All @@ -7,6 +8,7 @@
in {
flake.darwinConfigurations.macos-01 = inputs.darwin.lib.darwinSystem {
inherit system inputs;
inherit (self) specialArgs;
modules = [
./configuration.nix
../../nixos/macos.nix
Expand Down
2 changes: 2 additions & 0 deletions modules/flake-parts/darwinConfigurations.macos-02/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
self,
lib,
inputs,
...
Expand All @@ -7,6 +8,7 @@
in {
flake.darwinConfigurations.macos-02 = inputs.darwin.lib.darwinSystem {
inherit system inputs;
inherit (self) specialArgs;
modules = [
./configuration.nix
../../nixos/macos.nix
Expand Down
2 changes: 2 additions & 0 deletions modules/flake-parts/darwinConfigurations.macos-03/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
self,
lib,
inputs,
...
Expand All @@ -7,6 +8,7 @@
in {
flake.darwinConfigurations.macos-03 = inputs.darwin.lib.darwinSystem {
inherit system inputs;
inherit (self) specialArgs;
modules = [
./configuration.nix
../../nixos/macos.nix
Expand Down
2 changes: 2 additions & 0 deletions modules/flake-parts/darwinConfigurations.macos-04/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
self,
lib,
inputs,
...
Expand All @@ -7,6 +8,7 @@
in {
flake.darwinConfigurations.macos-04 = inputs.darwin.lib.darwinSystem {
inherit system inputs;
inherit (self) specialArgs;
modules = [
./configuration.nix
../../nixos/macos.nix
Expand Down
2 changes: 2 additions & 0 deletions modules/flake-parts/darwinConfigurations.macos-05/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
self,
lib,
inputs,
...
Expand All @@ -7,6 +8,7 @@
in {
flake.darwinConfigurations.macos-05 = inputs.darwin.lib.darwinSystem {
inherit system inputs;
inherit (self) specialArgs;
modules = [
./configuration.nix
../../nixos/macos.nix
Expand Down
2 changes: 2 additions & 0 deletions modules/flake-parts/darwinConfigurations.macos-06/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
self,
lib,
inputs,
...
Expand All @@ -7,6 +8,7 @@
in {
flake.darwinConfigurations.macos-06 = inputs.darwin.lib.darwinSystem {
inherit system inputs;
inherit (self) specialArgs;
modules = [
./configuration.nix
../../nixos/macos.nix
Expand Down
18 changes: 17 additions & 1 deletion modules/nixos/holo-deploy.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{lib, ...}: {
{
self,
lib,
...
}: {
options = {
deployUser = lib.mkOption {
type = lib.types.str;
Expand All @@ -10,4 +14,16 @@
description = "IP addres or host name to connect to the host";
};
};

config = {
environment.etc."nix/sourceInfo.json" = {
enable = true;
text = builtins.toJSON (builtins.removeAttrs self.sourceInfo ["outPath"]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the outPath attribute isn't removed the file would only contain the outPath i.e. nix-store path of the sources, which isn't helpful in this use-case.

};

environment.etc."nix/source" = {
enable = true;
text = self.sourceInfo.outPath;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be a text file that contains the path to the sources stored underneath /nix/store/...

};
};
}