diff --git a/README.md b/README.md index 9365db4..2dacfbf 100644 --- a/README.md +++ b/README.md @@ -35,14 +35,20 @@ vim ./flake.nix 4. **Ready** to go: ``` -nix develop +nix develop -c $SHELL ``` **Optional**: If you'd like to add **additional packages** to your new tritonshell, you -can do so: just edit `flake.nix` and look for `extraDevshellPkgs`. Packages +can do so: just edit `flake.nix` and look for `devshell.packages`. Packages added here will be pulled from the `nixos-unstable` channel, you can [search for available packages here](https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=hello). -TODO: add `nix-direnv` +You can use `nix-direnv` with this template, [read here how to install it](https://github.com/nix-community/nix-direnv#installation). +Then, just run: +``` +direnv allow +``` +in the newly created `./tritonshell` directory to automatically rebuild the +flake upon changes to the `flake.nix` file. diff --git a/template/flake.lock b/flake.lock similarity index 93% rename from template/flake.lock rename to flake.lock index c2b460f..159e757 100644 --- a/template/flake.lock +++ b/flake.lock @@ -67,11 +67,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1664538465, - "narHash": "sha256-EnlC7dDKX7X1wlnXkB1gmn9rBZQ0J9+biVTZHw//8us=", + "lastModified": 1665349835, + "narHash": "sha256-UK4urM3iN80UXQ7EaOappDzcisYIuEURFRoGQ/yPkug=", "owner": "nixos", "repo": "nixpkgs", - "rev": "10ecda252ce1b3b1d6403caeadbcc8f30d5ab796", + "rev": "34c5293a71ffdb2fe054eb5288adc1882c1eb0b1", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index d6632d0..427ed7a 100644 --- a/flake.nix +++ b/flake.nix @@ -1,10 +1,78 @@ { - description = "nix flake template for devs & ops environment with triton"; + description = "devshell nix module for triton DevOps shell environment"; - outputs = { self }: { - defaultTemplate = { - description = "nix flake new --template 'git+https://git.greenbaum.cloud/greenbaum.cloud/tritonshell?ref=main' ./tritonshell"; - path = ./template; - }; - }; + inputs.devshell.url = "github:numtide/devshell"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + + outputs = { self, flake-utils, devshell, nixpkgs }: + flake-utils.lib.eachSystem [ + "aarch64-linux" + "i686-linux" + "x86_64-darwin" + "x86_64-linux" + ] + ( + system: + let + pkgs = import nixpkgs { + inherit system; + + overlays = [ + devshell.overlay + (import ./overlay.nix) + ]; + }; + in + { + devshellModules.tritonshell = { config, lib, ... }: + with lib; + { + options = { + environment = { + enable = mkEnableOption "triton DevOps shell environment"; + # options used to set triton env vars in tritonshell + # adjust to suit your Triton Data Center setup + # take a look at ./pkgs/triton-docker-env-shell.nix to see how these get used + cnsBaseDomain = mkOption { + description = "Base domain for generated CNS domain records"; + type = types.nullOr types.str; + default = "greenbaum.zone"; + }; + dataCenters = mkOption { + description = "List of available triton data centers"; + type = types.listOf types.str; + default = [ "cgn-1" "lev-1" ]; + }; + mantaDomain = mkOption { + description = "Domain for manta object storage service"; + type = types.nullOr types.str; + default = "eu-central.manta.greenbaum.cloud"; + }; + tritonApiDomain = mkOption { + description = "Domain for triton API"; + type = types.nullOr types.str; + default = "api.greenbaum.cloud"; + }; + }; + }; + + config = + lib.mkIf config.environment.enable + (import ./tritonshell.nix { inherit config devshell pkgs self system; }); + }; + # Internal utility package with shell function and env vars helper. + # These get source'd in devshell.bash.extra when starting tritonshell + packages = { + triton-utils = import ./pkgs/triton-utils.nix { inherit pkgs; }; + triton-docker-env = import ./pkgs/triton-docker-env.nix { inherit pkgs; }; + }; + outputs = { self }: { + defaultTemplate = { + description = "nix flake new --template 'git+https://git.greenbaum.cloud/greenbaum.cloud/tritonshell?ref=main' ./tritonshell"; + path = ./template; + }; + }; + } + ); } diff --git a/template/overlay.nix b/overlay.nix similarity index 100% rename from template/overlay.nix rename to overlay.nix diff --git a/pkgs/triton-docker-env-shell.nix b/pkgs/triton-docker-env-shell.nix new file mode 100644 index 0000000..15e4b19 --- /dev/null +++ b/pkgs/triton-docker-env-shell.nix @@ -0,0 +1,36 @@ +{ pkgs, ... }: +with pkgs.nodePackages; +'' + # Script to set the docker, triton, manta and CNS env vars for the current + # triton profile + # Docs: https://docs.greenbaum.cloud/en/devops/triton-cli.html + # triton CLI source: https://github.com/tritonDataCenter/node-triton + cnsBaseDomain=$TRITONSHELL_CNS_BASE_DOMAIN + dataCenters=$TRITONSHELL_DATA_CENTERS + mantaDomain=$TRITONSHELL_MANTA_DOMAIN + tritonApiDomain=$TRITONSHELL_TRITON_API_DOMAIN + + # Set triton and docker host environment variables + eval "$(${triton}/bin/triton env)" + + # Get the user's UUID + triton_account_uuid="$(${triton}/bin/triton account get --json | ${json}/bin/json id)" + + # Set the CNS (container name service) base domain for auto-generated DNS + # records in public and private networks + # Note: the defaults are configured to work with our data centers, adjust the + # nix module options like config.environment.cnsBaseDomain in your devshell + # if you'd like to use tritonshell with your own, self-hosted TritonDataCenter + for dc in "''${dataCenters[@]}"; do + if env | grep -q -E "SDC_URL=https://''${dc}.''${tritonApiDomain}"; then + export \ + TRITON_CNS_SEARCH_DOMAIN_PUBLIC="''${triton_account_uuid}.''${dc}.''${cnsBaseDomain}" \ + TRITON_CNS_SEARCH_DOMAIN_PRIVATE="''${triton_account_uuid}.''${dc}.int.''${cnsBaseDomain}" \ + TRITON_DC=''$dc + fi + done + + export MANTA_URL=https://''${mantaDomain} + export MANTA_USER=$SDC_ACCOUNT + export MANTA_KEY_ID=$SDC_KEY_ID +'' diff --git a/pkgs/triton-docker-env.nix b/pkgs/triton-docker-env.nix new file mode 100644 index 0000000..6cd8c8c --- /dev/null +++ b/pkgs/triton-docker-env.nix @@ -0,0 +1,2 @@ +{ pkgs, ... }: +(pkgs.writeShellScriptBin "triton-docker-env.sh" (import ./triton-docker-env-shell.nix { inherit pkgs; })) diff --git a/template/pkgs/triton-utils.nix b/pkgs/triton-utils.nix similarity index 100% rename from template/pkgs/triton-utils.nix rename to pkgs/triton-utils.nix diff --git a/template/pkgs/utils/cacert-2022-07-19.pem b/pkgs/utils/cacert-2022-07-19.pem similarity index 100% rename from template/pkgs/utils/cacert-2022-07-19.pem rename to pkgs/utils/cacert-2022-07-19.pem diff --git a/template/pkgs/utils/ttp.sh b/pkgs/utils/ttp.sh similarity index 100% rename from template/pkgs/utils/ttp.sh rename to pkgs/utils/ttp.sh diff --git a/template/pkgs/utils/unset-env.sh b/pkgs/utils/unset-env.sh similarity index 100% rename from template/pkgs/utils/unset-env.sh rename to pkgs/utils/unset-env.sh diff --git a/template/.envrc b/template/.envrc new file mode 100644 index 0000000..80ffa0d --- /dev/null +++ b/template/.envrc @@ -0,0 +1,3 @@ +# reload when these files change +watch_file flake.nix +use_flake diff --git a/template/.gitignore b/template/.gitignore new file mode 100644 index 0000000..2bbdbfe --- /dev/null +++ b/template/.gitignore @@ -0,0 +1,2 @@ +.direnv +result diff --git a/template/flake.nix b/template/flake.nix index 57ab9bc..9611593 100644 --- a/template/flake.nix +++ b/template/flake.nix @@ -1,65 +1,44 @@ { - description = "devs & ops environment for nix'ing with triton"; + description = "Example devshell using the tritonshell nix module"; - inputs.devshell.url = "github:numtide/devshell"; - inputs.flake-utils.url = "github:numtide/flake-utils"; - inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - outputs = { self, flake-utils, devshell, nixpkgs }: - flake-utils.lib.eachSystem [ - "aarch64-linux" - "i686-linux" - "x86_64-darwin" - "x86_64-linux" - ] - (system: - let - pkgs = import nixpkgs { - inherit system; - }; - in - { - # Internal utility package with shell function and env vars helper. - # These get source'd in devshell.bash.extra when starting tritonshell - packages = { - triton-utils = import ./pkgs/triton-utils.nix { inherit pkgs; }; - triton-docker-env = - let - # CUSTOMIZE: - # variables used to set triton env vars in tritonshell - # adjust to suit your Triton Data Center setup - # take a look at ./pkgs/triton-docker-env-shell.nix to see how these get used - cnsBaseDomain = "greenbaum.zone"; - dataCenters = [ "cgn-1" "lev-1" ]; - mantaDomain = "eu-central.manta.greenbaum.cloud"; - tritonApiDomain = "api.greenbaum.cloud"; - in - import ./pkgs/triton-docker-env.nix { - inherit pkgs cnsBaseDomain dataCenters mantaDomain tritonApiDomain; - }; + flake-utils.url = "github:numtide/flake-utils"; + + devshell.url = "github:numtide/devshell"; + devshell.inputs.flake-utils.follows = "flake-utils"; + devshell.inputs.nixpkgs.follows = "nixpkgs"; + + tritonshell-module.url = "git+https://git.greenbaum.cloud/dev/tritonshell?ref=main"; + tritonshell-module.inputs.devshell.follows = "devshell"; + tritonshell-module.inputs.flake-utils.follows = "flake-utils"; + tritonshell-module.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { self, flake-utils, devshell, tritonshell-module, nixpkgs }: + flake-utils.lib.simpleFlake { + inherit self nixpkgs; + name = "infra-project"; + preOverlays = [ devshell.overlay ]; + shell = { pkgs }: + pkgs.devshell.mkShell { + imports = [ tritonshell-module.devshellModules.x86_64-linux.tritonshell ]; + # Now the tritonshell environment nix module options are available + environment = { + enable = true; + # CUSTOMIZE if desired, default options are: + #cnsBaseDomain = "greenbaum.zone"; + #dataCenters = [ "cgn-1" "lev-1" ]; + #mantaDomain = "eu-central.manta.greenbaum.cloud"; + #tritonApiDomain = "api.greenbaum.cloud"; }; - devShells.default = - let - pkgs = import nixpkgs { - inherit system; - - overlays = [ - devshell.overlay - (import ./overlay.nix) - ]; - }; - - # HINT: add your extra devshell pkgs here, use any packages you want - # available in your devshell's PATH - # Use https://search.nixos.org/packages to find available packages - # in the unstable channel - # These get appended to devshell.packages in ./tritonshell.nix - extraDevshellPkgs = with pkgs; [ - #hello - ]; - - in - import ./tritonshell.nix { inherit extraDevshellPkgs devshell pkgs self system; }; - }); + # Add additional packages you'd like to be available in your devshell + # PATH here + devshell.packages = with pkgs; [ + #nodejs + ]; + }; + }; } diff --git a/template/pkgs/triton-docker-env-shell.nix b/template/pkgs/triton-docker-env-shell.nix deleted file mode 100644 index 55e0dae..0000000 --- a/template/pkgs/triton-docker-env-shell.nix +++ /dev/null @@ -1,31 +0,0 @@ -{ pkgs, cnsBaseDomain, dataCenters, mantaDomain, tritonApiDomain, ... }: -with pkgs.nodePackages; -'' - export PATH="${triton}/bin:${json}/bin:$PATH" - # script to set the docker, triton, manta and CNS env vars for the current - # triton profile - - # set triton and docker host environment variables - eval "$(triton env)" - - # get the user's UUID - triton_account_uuid="$(triton account get --json | json id)" - - # set the CNS (container name service) base for auto-generated DNS records - # in public and private networks - # note, this makes assumptions that only work if you configured the nix - # variables in flake.nix "CUSTOMIZE" section according to your Triton Data - # Center setup - for dc in ${pkgs.lib.concatStringsSep " " dataCenters}; do - if env | grep -q -E "SDC_URL=https://''${dc}.${tritonApiDomain}"; then - export \ - TRITON_CNS_SEARCH_DOMAIN_PUBLIC="''${triton_account_uuid}.''${dc}.${cnsBaseDomain}" \ - TRITON_CNS_SEARCH_DOMAIN_PRIVATE="''${triton_account_uuid}.''${dc}.int.${cnsBaseDomain}" \ - TRITON_DC=''$dc - fi - done - - export MANTA_URL=https://${mantaDomain} - export MANTA_USER=$SDC_ACCOUNT - export MANTA_KEY_ID=$SDC_KEY_ID -'' diff --git a/template/pkgs/triton-docker-env.nix b/template/pkgs/triton-docker-env.nix deleted file mode 100644 index 416e2c7..0000000 --- a/template/pkgs/triton-docker-env.nix +++ /dev/null @@ -1,2 +0,0 @@ -{ pkgs, cnsBaseDomain, dataCenters, mantaDomain, tritonApiDomain, ... }: -(pkgs.writeShellScriptBin "triton-docker-env.sh" (import ./triton-docker-env-shell.nix { inherit pkgs cnsBaseDomain dataCenters mantaDomain tritonApiDomain; })) diff --git a/template/tritonshell.nix b/tritonshell.nix similarity index 83% rename from template/tritonshell.nix rename to tritonshell.nix index f0f47dd..47d647c 100644 --- a/template/tritonshell.nix +++ b/tritonshell.nix @@ -1,5 +1,12 @@ -{ extraDevshellPkgs, devshell, pkgs, self, system, ... }: -pkgs.devshell.mkShell { +{ config, devshell, pkgs, self, system, ... }: +let + tritonConfig = config.environment; + cnsBaseDomain = tritonConfig.cnsBaseDomain; + dataCenters = tritonConfig.dataCenters; + mantaDomain = tritonConfig.mantaDomain; + tritonApiDomain = tritonConfig.tritonApiDomain; +in +{ # devshell docs: https://numtide.github.io/devshell/modules_schema.html name = "tritonshell"; @@ -60,9 +67,10 @@ pkgs.devshell.mkShell { self.packages.${system}.triton-utils self.packages.${system}.triton-docker-env # useful for working with JSON data - jq bunyan-rs - ] ++ extraDevshellPkgs; + jq + nodePackages.json + ]; env = [ # workaround for TLS certs bug in docker-compose, CERTIFICATE_VERIFY_FAILED @@ -87,6 +95,11 @@ pkgs.devshell.mkShell { bash = { extra = '' + export \ + TRITONSHELL_CNS_BASE_DOMAIN=${cnsBaseDomain} \ + TRITONSHELL_DATA_CENTERS=(${pkgs.lib.concatStringsSep " " dataCenters}) \ + TRITONSHELL_MANTA_DOMAIN=${mantaDomain} \ + TRITONSHELL_TRITON_API_DOMAIN=${tritonApiDomain} if [ -z "$TRITON_DONT_SOURCE_PROFILE" ]; then source ${self.packages.${system}.triton-docker-env}/bin/triton-docker-env.sh source ${self.packages.${system}.triton-utils}/bin/ttp.sh