Skip to content

Commit

Permalink
Merge pull request #1 from Janik-Haag/develop
Browse files Browse the repository at this point in the history
add service template things
  • Loading branch information
Janik-Haag authored Feb 15, 2024
2 parents dc58671 + 9ea9b41 commit c8fd210
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser, Subcommand};
use serde::{Serialize, Deserialize};
use tvix_serde;
// use tvix_serde;
use std::net::IpAddr;
use std::{env, path::PathBuf};
use std::error;
Expand Down Expand Up @@ -43,7 +43,7 @@ fn netbox_api_request(url: String) -> Result<String, Box<dyn error::Error>> {
let client = reqwest::blocking::Client::new();

// We can't use the bearer auth method because netbox doesn't comply with the standards.
let resp = client.get(&url).header("Authorization", "Token ".to_owned() + &token).send()?;
let resp = client.get(url).header("Authorization", "Token ".to_owned() + &token).send()?;
let json = resp.json::<serde_json::Value>()?;
Ok(json.to_string())
}
Expand All @@ -59,7 +59,7 @@ struct Nix {
// the code here is just a filler example to make it compile happily
fn get_current_config() -> Result<Nix, Box<dyn error::Error>> {
let deserialized: Result<Nix, Box<dyn error::Error>> = Ok(tvix_serde::from_str::<Nix>("lib.attrsets.recursiveUpdate { a = \"coding with tvix!\"; } { b = 231; }")?);
return deserialized;
deserialized
}

fn add(ips: bool) -> Result<(), Box<dyn error::Error>> {
Expand Down
6 changes: 6 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
};
}
);
utils = import ./utils { inherit (nixpkgs) lib; };
nixosModules = rec {
nixbox = import ./modules/nixbox.nix { inherit (self) utils; };
default = nixbox;
};
formatter = forAllSystems (
system:
nixpkgs.legacyPackages.${system}.nixpkgs-fmt
Expand All @@ -58,6 +63,7 @@

# nixbox-cli
cargo
clippy
];
};
});
Expand Down
6 changes: 6 additions & 0 deletions modules/nixbox.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ utils }: { lib, ... }:
{
imports = [
./service-template.nix
];
}
36 changes: 36 additions & 0 deletions modules/service-template.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{ config, lib, utils }:
{
options = {
nixbox = {
serviceTemplate = lib.types.attrsOf (lib.types.submodule {
options = {
name = lib.mkOption {
description = ''
A service or protocol name.
'';
type = lib.types.str;
example = "https";
};
protocol = lib.mkOption {
description = ''
The wire protocol on which the service runs.
'';
type = lib.types.enum [ "tcp" "udp" "sctp" ];
example = "tcp";
};
ports = lib.mkOption {
description = ''
One or more numeric ports to which the service is bound. Multiple ports can be expressed using lists and `utils.networking.portrange`.
'';
type = lib.types.listOf lib.types.int;
example = [ 443 ];
};
};
});
servicePrests = { };
};
};
config = {
nixbox = { };
};
}
11 changes: 11 additions & 0 deletions utils/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{ lib }:
let
utils = lib.makeExtensible (self:
let
callUtils = file: import file { inherit lib; utils = self; };
in
{
networking = callUtils ./networking.nix;
});
in
utils
26 changes: 26 additions & 0 deletions utils/networking.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ lib, ... }: {
networking = {
/*
Generates inclusive portrange given a lower and a higher number
Type:
utils.networking.portrange:: Int -> Int -> [ Int ]
*/
portrange =
from:
to:
let
helper = input:
if input.counter >= 0 then
helper
{
list = [ (from + input.counter) ] ++ input.list;
counter = input.counter - 1;
} else input.list;
in
lib.throwIf (from > to) "the second input has to be larger then the first one, otherwise you have a negative range which is impossible or not a range."
helper
{ list = [ ]; counter = (to - from); }
;
};
}

0 comments on commit c8fd210

Please sign in to comment.