From e9a6d11bbc277233bcb24f5bee23d7136abdf784 Mon Sep 17 00:00:00 2001 From: "Janik H." Date: Fri, 16 Feb 2024 18:52:29 +0100 Subject: [PATCH] add clippy check to ci --- cli/src/main.rs | 39 +++++++++------- flake.nix | 115 ++++++++++++++++++++++++++++-------------------- 2 files changed, 90 insertions(+), 64 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index cec2c12..0363acf 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,11 +1,11 @@ use clap::{Parser, Subcommand}; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; // use tvix_serde; -use std::net::IpAddr; -use std::{env, path::PathBuf}; use std::error; use std::fs; use std::io::Write; +use std::net::IpAddr; +use std::{env, path::PathBuf}; #[derive(Parser, Debug)] #[clap(version)] @@ -32,9 +32,9 @@ enum SubCommands { #[derive(Debug, Serialize, Deserialize)] struct IpAddresses { address: IpAddr, // TODO(Janik): make it use CIDR - status: String, // TODO(Janik): enum actual netbox values + status: String, // TODO(Janik): enum actual netbox values dns_name: String, - tags: Vec + tags: Vec, } fn netbox_api_request(url: String) -> Result> { @@ -43,7 +43,10 @@ fn netbox_api_request(url: String) -> Result> { 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::()?; Ok(json.to_string()) } @@ -51,29 +54,33 @@ fn netbox_api_request(url: String) -> Result> { #[derive(Serialize, Deserialize, Debug)] struct Nix { a: String, - b: u32 + b: u32, } // this is currently stuck on https://cl.tvl.fyi/c/depot/+/10581 // because we need import functionality to pull in and eval the users config // the code here is just a filler example to make it compile happily -fn get_current_config() -> Result> { - let deserialized: Result> = Ok(tvix_serde::from_str::("lib.attrsets.recursiveUpdate { a = \"coding with tvix!\"; } { b = 231; }")?); - deserialized -} +// fn get_current_config() -> Result> { +// let deserialized: Result> = Ok(tvix_serde::from_str::("lib.attrsets.recursiveUpdate { a = \"coding with tvix!\"; } { b = 231; }")?); +// deserialized +// } -fn add(ips: bool) -> Result<(), Box> { - if ips { - } +fn add(_ips: bool) -> Result<(), Box> { + // if ips {} Ok(()) } -fn export(ips: bool, location: Option) -> Result<(), Box>{ +fn export(ips: bool, location: Option) -> Result<(), Box> { let location = location.unwrap_or_else(|| PathBuf::from("/tmp/nixbox_export")); fs::create_dir_all(location.clone())?; if ips { let mut ips_file = fs::File::create(location.to_str().unwrap().to_owned() + "ips.json")?; - let _ = ips_file.write_all((netbox_api_request(String::from("https://demo.netbox.dev/api/ipam/ip-addresses"))?).as_bytes()); + let _ = ips_file.write_all( + (netbox_api_request(String::from( + "https://demo.netbox.dev/api/ipam/ip-addresses", + ))?) + .as_bytes(), + ); } Ok(()) } diff --git a/flake.nix b/flake.nix index ebaa1e7..a6540d1 100644 --- a/flake.nix +++ b/flake.nix @@ -5,68 +5,87 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; flake-compat.url = "github:edolstra/flake-compat"; flake-compat.flake = false; + flake-utils.url = "github:numtide/flake-utils"; crane.url = "github:ipetkov/crane"; crane.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = { self, nixpkgs, flake-compat, crane }: - let - forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; - in - { - packages = forAllSystems (system: - let - pkgs = nixpkgs.legacyPackages.${system}; - craneLib = crane.lib.${system}; - in - rec { - default = cli; - dataSourceDemo = (import ./utils/builder.nix { inherit (nixpkgs) lib; pkgs = nixpkgs.legacyPackages.${system}; }).dataSource { }; - cli = craneLib.buildPackage { - src = craneLib.cleanCargoSource (craneLib.path ./cli); - strictDeps = true; + outputs = { self, nixpkgs, flake-compat, flake-utils, crane }: + (flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + craneLib = crane.lib.${system}; - # Needed to get openssl-sys to use pkg-config. - OPENSSL_NO_VENDOR = 1; + # Common derivation arguments used for all builds + cliCommonArgs = { + src = craneLib.cleanCargoSource (craneLib.path ./cli); + strictDeps = true; - buildInputs = with pkgs; [ - openssl - ]; + # Needed to get openssl-sys to use pkg-config. - nativeBuildInputs = with pkgs; [ - pkg-config - ]; + OPENSSL_NO_VENDOR = 1; + + buildInputs = with pkgs; [ + openssl + ]; + + nativeBuildInputs = with pkgs; [ + pkg-config + ]; + }; + cliCargoArtifacts = craneLib.buildDepsOnly (cliCommonArgs // { + pname = "nixbox-cli-deps"; + }); + cliClippy = craneLib.cargoClippy (cliCommonArgs // { + cargoArtifacts = cliCargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + cliCrate = craneLib.buildPackage (cliCommonArgs // { + cargoArtifacts = cliCargoArtifacts; + }); + + cliCoverage = craneLib.cargoTarpaulin (cliCommonArgs // { + cargoArtifacts = cliCargoArtifacts; + }); + in + { + packages = + { + default = cliCrate; + cli = cliCrate; + dataSourceDemo = (import ./utils/builder.nix { inherit (nixpkgs) lib; pkgs = nixpkgs.legacyPackages.${system}; }).dataSource { }; }; - } - ); - 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 - ); - tests = nixpkgs.lib.mapAttrs (name: v: import "${./utils}/tests/${name}.nix" { inherit self; inherit (nixpkgs) lib; inherit (self) utils; }) (builtins.removeAttrs self.utils [ "__unfix__" "extend" "generate" ]); - devShells = forAllSystems (system: - let - pkgs = nixpkgs.legacyPackages.${system}; - in - { - default = pkgs.mkShell { - buildInputs = with pkgs; [ + formatter = nixpkgs.legacyPackages.${system}.nixpkgs-fmt + ; + devShells = { + default = craneLib.devShell { + checks = self.checks.${system}; + inputsFrom = [ + cliCrate + ]; + packages = with pkgs; [ nixpkgs-fmt nix-unit nixdoc statix - - # nixbox-cli - cargo - clippy ]; }; - }); + }; + checks = { + inherit + cliCrate + cliClippy + # cliCoverage // not yet used + ; + }; + })) // { + tests = nixpkgs.lib.mapAttrs (name: v: import "${./utils}/tests/${name}.nix" { inherit self; inherit (nixpkgs) lib; inherit (self) utils; }) (builtins.removeAttrs self.utils [ "__unfix__" "extend" "generate" ]); + utils = import ./utils { inherit (nixpkgs) lib; }; + nixosModules = rec { + nixbox = import ./modules/nixbox.nix { inherit (self) utils; }; + default = nixbox; + }; }; }