Skip to content

Commit

Permalink
Simplify dev setup by moving commons to another flake
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek committed Mar 12, 2024
1 parent ad8c1cc commit b64caf9
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 438 deletions.
73 changes: 71 additions & 2 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 37 additions & 74 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,129 +2,92 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-23.11";
flake-utils.url = "github:numtide/flake-utils";
devshell-tools.url = "github:eikek/devshell-tools";
};

outputs = inputs @ {
self,
nixpkgs,
flake-utils,
devshell-tools,
}:
{
overlays.default = final: prev: {
solr = self.packages.${prev.system}.solr;
openapi-doc = self.packages.${prev.system}.openapi-doc;
};
nixosConfigurations = let
selfOverlay = {
lib,
config,
...
}: {
nixpkgs.overlays = [
self.overlays.default
];
system.stateVersion = "23.11";
};
in {
dev-vm = nixpkgs.lib.nixosSystem {
nixosConfigurations = {
rsdev-vm = devshell-tools.lib.mkVm {
system = flake-utils.lib.system.x86_64-linux;
specialArgs = {inherit inputs;};
modules = [
selfOverlay
./nix/dev-vm.nix
./nix/services.nix
];
};

container = nixpkgs.lib.nixosSystem {
rsdev-cnt = devshell-tools.lib.mkContainer {
system = flake-utils.lib.system.x86_64-linux;
modules = [
({pkgs, ...}: {
boot.isContainer = true;
networking.useDHCP = false;
})
selfOverlay
./nix/solr-module.nix
./nix/services.nix
];
};
};
}
// flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
selfPkgs = self.packages.${system};
selfPkgs = import ./nix/dev-scripts.nix {
inherit system devshell-tools;
inherit (pkgs) writeShellScriptBin;
};
devshellToolsPkgs = devshell-tools.packages.${system};
commonPackages = with pkgs;
[
redis
jq
scala-cli
devshellToolsPkgs.sbt17
devshellToolsPkgs.openapi-docs
]
++ (builtins.attrValues selfPkgs);
in {
formatter = pkgs.alejandra;
packages =
((import ./nix/dev-scripts.nix) {inherit (pkgs) concatTextFile writeShellScriptBin;})
// rec {
solr = pkgs.callPackage (import ./nix/solr.nix) {};
swagger-ui = pkgs.callPackage (import ./nix/swagger-ui.nix) {};
openapi-doc = pkgs.callPackage (import ./nix/openapi-doc.nix) {inherit swagger-ui;};
};

devShells = rec {
default = container;
container = pkgs.mkShell {
RS_SOLR_HOST = "rsdev";
RS_SOLR_URL = "http://rsdev:8983/solr";
container = pkgs.mkShellNoCC {
RS_SOLR_HOST = "rsdev-cnt";
RS_SOLR_URL = "http://rsdev-cnt:8983/solr";
RS_SOLR_CORE = "rsdev-test";
RS_REDIS_HOST = "rsdev";
RS_REDIS_HOST = "rsdev-cnt";
RS_REDIS_PORT = "6379";
RS_CONTAINER = "rsdev";
RS_LOG_LEVEL = "3";
RS_REDIS_QUEUE_projectCreated = "projectCreated";
RS_REDIS_QUEUE_userAdded = "userAdded";

#don't start docker container for dbTests
NO_SOLR = "true";
NO_REDIS = "true";
DEV_CONTAINER = "rsdev-cnt";

buildInputs = with pkgs;
with selfPkgs; [
redis
jq
sbt
scala-cli

redis-push
recreate-container
start-container
solr-create-core
solr-delete-core
solr-recreate-core
solr-recreate-dbtests-cores
solr-logs
k8s-reprovision
];
buildInputs =
commonPackages
++ (builtins.attrValues devshell-tools.legacyPackages.${system}.cnt-scripts);
};
vm = pkgs.mkShell {
vm = pkgs.mkShellNoCC {
RS_SOLR_URL = "http://localhost:18983/solr";
RS_SOLR_CORE = "rsdev-test";
RS_REDIS_HOST = "localhost";
RS_REDIS_PORT = "16379";
VM_SSH_PORT = "10022";
RS_LOG_LEVEL = "3";
RS_REDIS_QUEUE_projectCreated = "projectCreated";
RS_REDIS_QUEUE_userAdded = "userAdded";

#don't start docker container for dbTests
NO_SOLR = "true";
NO_REDIS = "true";

buildInputs = with pkgs;
with selfPkgs; [
redis
jq
sbt
scala-cli
DEV_VM = "rsdev-vm";

redis-push
vm-build
vm-run
vm-ssh
vm-solr-create-core
vm-solr-delete-core
vm-solr-recreate-core
vm-solr-logs
solr-recreate-dbtests-cores
k8s-reprovision
];
buildInputs =
commonPackages
++ (builtins.attrValues devshell-tools.legacyPackages.${system}.vm-scripts);
};
};
});
Expand Down
99 changes: 18 additions & 81 deletions nix/dev-scripts.nix
Original file line number Diff line number Diff line change
@@ -1,94 +1,31 @@
{
concatTextFile,
system,
writeShellScriptBin,
}: let key = ./dev-vm-key; in rec {
redis-push = concatTextFile {
name = "redis-push";
files = [./scripts/redis-push];
executable = true;
destination = "/bin/redis-push";
devshell-tools,
}: rec {
redis-push = devshell-tools.lib.installScript {
script = ./scripts/redis-push;
inherit system;
};

recreate-container = concatTextFile {
name = "recreate-container";
files = [./scripts/recreate-container];
executable = true;
destination = "/bin/recreate-container";
k8s-reprovision = devshell-tools.lib.installScript {
script = ./scripts/k8s-reprovision;
inherit system;
};

start-container = writeShellScriptBin "start-container" ''
cnt=''${RS_CONTAINER:-rsdev}
sudo nixos-container start $cnt
'';

solr-create-core = writeShellScriptBin "solr-create-core" ''
core_name=''${1:-rsdev-test}
sudo nixos-container run ''${RS_CONTAINER:-rsdev} -- su solr -c "solr create -c $core_name"
sudo nixos-container run ''${RS_CONTAINER:-rsdev} -- find /var/solr/data/$core_name/conf -type f -exec chmod 644 {} \;
'';

solr-delete-core = writeShellScriptBin "solr-delete-core" ''
core_name=''${1:-rsdev-test}
sudo nixos-container run ''${RS_CONTAINER:-rsdev} -- su solr -c "solr delete -c $core_name"
'';

solr-recreate-core = writeShellScriptBin "solr-recreate-core" ''
${solr-delete-core}/bin/solr-delete-core "$1"
${solr-create-core}/bin/solr-create-core "$1"
'';

solr-logs = writeShellScriptBin "solr-logs" ''
sudo nixos-container run ''${RS_CONTAINER:-rsdev} -- journalctl -efu solr.service
'';

vm-build = writeShellScriptBin "vm-build" ''
nix build .#nixosConfigurations.dev-vm.config.system.build.vm
'';

vm-run = writeShellScriptBin "vm-run" ''
nix run .#nixosConfigurations.dev-vm.config.system.build.vm
'';

vm-ssh = writeShellScriptBin "vm-ssh" ''
ssh -i ${key} -p $VM_SSH_PORT root@localhost "$@"
'';

vm-solr-logs = writeShellScriptBin "solr-logs" ''
${vm-ssh}/bin/vm-ssh journalctl -efu solr.service
'';

vm-solr-create-core = writeShellScriptBin "solr-create-core" ''
core_name=''${1:-rsdev-test}
ssh -p $VM_SSH_PORT root@localhost "su solr -c \"solr create -c $core_name\""
ssh -p $VM_SSH_PORT root@localhost "find /var/solr/data/$core_name/conf -type f -exec chmod 644 {} \;"
'';

vm-solr-delete-core = writeShellScriptBin "solr-delete-core" ''
core_name=''${1:-rsdev-test}
ssh -p $VM_SSH_PORT root@localhost "su solr -c \"solr delete -c $core_name\""
'';

vm-solr-recreate-core = writeShellScriptBin "solr-recreate-core" ''
${vm-solr-delete-core}/bin/solr-delete-core "$1"
${vm-solr-create-core}/bin/solr-create-core "$1"
solr-recreate-cores = writeShellScriptBin "solr-recreate-cores" ''
script=$([ "$(which cnt-solr-recreate-core)" != "" ] && echo "cnt-solr-recreate-core" || echo "vm-solr-recreate-core")
for c in $@; do
$script $c
done
'';

# core names are defined in project/SolrServer.scala
solr-recreate-dbtests-cores = writeShellScriptBin "solr-recreate-dbtests-cores" ''
solr-delete-core core-test1
solr-delete-core core-test2
solr-delete-core core-test3
solr-delete-core search-core-test
solr-create-core core-test1
solr-create-core core-test2
solr-create-core core-test3
solr-create-core search-core-test
${solr-recreate-cores}/bin/solr-recreate-cores core-test1 core-test2 core-test3 search-core-test
'';

k8s-reprovision = concatTextFile {
name = "k8s-reprovision";
files = [./scripts/k8s-reprovision];
executable = true;
destination = "/bin/k8s-reprovision";
};
solr-recreate-search-core = writeShellScriptBin "solr-recreate-search-core" ''
${solr-recreate-cores}/bin/solr-recreate-cores $RS_SOLR_CORE
'';
}
7 changes: 0 additions & 7 deletions nix/dev-vm-key

This file was deleted.

1 change: 0 additions & 1 deletion nix/dev-vm-key.pub

This file was deleted.

Loading

0 comments on commit b64caf9

Please sign in to comment.