Merge pull request 'Restructure flake to make use of nix module system' (#1) from restructure into main

Reviewed-on: https://git.greenbaum.cloud/dev/tritonshell/pulls/1
This commit is contained in:
Jhonas Wernery 2022-10-12 14:37:28 +02:00
commit 61586ea666
16 changed files with 185 additions and 109 deletions

View file

@ -35,14 +35,20 @@ vim ./flake.nix
4. **Ready** to go: 4. **Ready** to go:
``` ```
nix develop nix develop -c $SHELL
``` ```
**Optional**: **Optional**:
If you'd like to add **additional packages** to your new tritonshell, you 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 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). 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.

View file

@ -67,11 +67,11 @@
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1664538465, "lastModified": 1665349835,
"narHash": "sha256-EnlC7dDKX7X1wlnXkB1gmn9rBZQ0J9+biVTZHw//8us=", "narHash": "sha256-UK4urM3iN80UXQ7EaOappDzcisYIuEURFRoGQ/yPkug=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "10ecda252ce1b3b1d6403caeadbcc8f30d5ab796", "rev": "34c5293a71ffdb2fe054eb5288adc1882c1eb0b1",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -1,10 +1,78 @@
{ {
description = "nix flake template for devs & ops environment with triton"; description = "devshell nix module for triton DevOps shell environment";
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 }: { outputs = { self }: {
defaultTemplate = { defaultTemplate = {
description = "nix flake new --template 'git+https://git.greenbaum.cloud/greenbaum.cloud/tritonshell?ref=main' ./tritonshell"; description = "nix flake new --template 'git+https://git.greenbaum.cloud/greenbaum.cloud/tritonshell?ref=main' ./tritonshell";
path = ./template; path = ./template;
}; };
}; };
}
);
} }

View file

@ -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
''

View file

@ -0,0 +1,2 @@
{ pkgs, ... }:
(pkgs.writeShellScriptBin "triton-docker-env.sh" (import ./triton-docker-env-shell.nix { inherit pkgs; }))

3
template/.envrc Normal file
View file

@ -0,0 +1,3 @@
# reload when these files change
watch_file flake.nix
use_flake

2
template/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.direnv
result

View file

@ -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 = {
inputs.flake-utils.url = "github:numtide/flake-utils"; nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
outputs = { self, flake-utils, devshell, nixpkgs }: flake-utils.url = "github:numtide/flake-utils";
flake-utils.lib.eachSystem [
"aarch64-linux" devshell.url = "github:numtide/devshell";
"i686-linux" devshell.inputs.flake-utils.follows = "flake-utils";
"x86_64-darwin" devshell.inputs.nixpkgs.follows = "nixpkgs";
"x86_64-linux"
] tritonshell-module.url = "git+https://git.greenbaum.cloud/dev/tritonshell?ref=main";
(system: tritonshell-module.inputs.devshell.follows = "devshell";
let tritonshell-module.inputs.flake-utils.follows = "flake-utils";
pkgs = import nixpkgs { tritonshell-module.inputs.nixpkgs.follows = "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;
};
}; };
devShells.default = outputs = { self, flake-utils, devshell, tritonshell-module, nixpkgs }:
let flake-utils.lib.simpleFlake {
pkgs = import nixpkgs { inherit self nixpkgs;
inherit system; 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";
};
overlays = [ # Add additional packages you'd like to be available in your devshell
devshell.overlay # PATH here
(import ./overlay.nix) devshell.packages = with pkgs; [
#nodejs
]; ];
}; };
};
# 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; };
});
} }

View file

@ -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
''

View file

@ -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; }))

View file

@ -1,5 +1,12 @@
{ extraDevshellPkgs, devshell, pkgs, self, system, ... }: { config, devshell, pkgs, self, system, ... }:
pkgs.devshell.mkShell { 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 # devshell docs: https://numtide.github.io/devshell/modules_schema.html
name = "tritonshell"; name = "tritonshell";
@ -60,9 +67,10 @@ pkgs.devshell.mkShell {
self.packages.${system}.triton-utils self.packages.${system}.triton-utils
self.packages.${system}.triton-docker-env self.packages.${system}.triton-docker-env
# useful for working with JSON data # useful for working with JSON data
jq
bunyan-rs bunyan-rs
] ++ extraDevshellPkgs; jq
nodePackages.json
];
env = [ env = [
# workaround for TLS certs bug in docker-compose, CERTIFICATE_VERIFY_FAILED # workaround for TLS certs bug in docker-compose, CERTIFICATE_VERIFY_FAILED
@ -87,6 +95,11 @@ pkgs.devshell.mkShell {
bash = { bash = {
extra = '' 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 if [ -z "$TRITON_DONT_SOURCE_PROFILE" ]; then
source ${self.packages.${system}.triton-docker-env}/bin/triton-docker-env.sh source ${self.packages.${system}.triton-docker-env}/bin/triton-docker-env.sh
source ${self.packages.${system}.triton-utils}/bin/ttp.sh source ${self.packages.${system}.triton-utils}/bin/ttp.sh