From 7d9f73a64243d6f394c3fcbf35662415a80a286c Mon Sep 17 00:00:00 2001 From: Stefan Ellmauthaler Date: Wed, 3 Aug 2022 22:11:20 +0200 Subject: [PATCH] Add templates to the default flake --- flake.nix | 19 +++++++++++- lib/files.nix | 18 +++++++++++ templates/basic_tools/.envrc | 1 + templates/basic_tools/flake.nix | 31 +++++++++++++++++++ templates/jupyter/flake.nix | 45 ++++++++++++++++++++++++++++ templates/rust/.envrc | 1 + templates/rust/.gitignore | 22 ++++++++++++++ templates/rust/flake.nix | 53 +++++++++++++++++++++++++++++++++ 8 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 templates/basic_tools/.envrc create mode 100644 templates/basic_tools/flake.nix create mode 100644 templates/jupyter/flake.nix create mode 100644 templates/rust/.envrc create mode 100644 templates/rust/.gitignore create mode 100644 templates/rust/flake.nix diff --git a/flake.nix b/flake.nix index 64a1515..80ad25c 100644 --- a/flake.nix +++ b/flake.nix @@ -65,7 +65,7 @@ (final: prev: { elss = (import ./lib { lib = final; }) prev; }); - inherit (extended-lib.elss) discoverModules moduleNames discoverMachines withModules; + inherit (extended-lib.elss) discoverModules moduleNames discoverMachines withModules discoverTemplates; in flake-utils-plus.lib.mkFlake rec{ inherit self inputs; @@ -144,5 +144,22 @@ sops-nix = inputs.sops-nix.packages."${channels.nixpkgs.system}"; }; }; + + templates = discoverTemplates ./templates { + basic_tool = { + description = "Basic setup of tools in nixpkgs/unstable"; + welcomeText = "Change into the folder and add the wanted packages to the buildInputs"; + }; + + rust = { + description = "Rust development environment flake"; + welcomeText = + "Change into the folder and follow the prompt to create an automatic rust environment in this folder"; + }; + jupyter = { + description = "Jupyter server flake"; + welcomeText = "Use `nix run .` to run a jupyter server instance."; + }; + }; }; } diff --git a/lib/files.nix b/lib/files.nix index 362c3f5..64ec074 100644 --- a/lib/files.nix +++ b/lib/files.nix @@ -21,4 +21,22 @@ with prev; rec { discoverMachines = dir: args: withModules dir ({ path, name }: { modules = [ path ]; } // args); + discoverTemplates = dir: overrides: + pipe dir [ + builtins.readDir + (filterAttrs (_name: type: type == "directory")) + attrNames + (map (template: + nameValuePair template (recursiveUpdate + { + path = "${dir}/${template}"; + description = "a template for ${template} projects"; + } + (if hasAttr template overrides then + getAttr template overrides + else + { })))) + listToAttrs + ]; + } diff --git a/templates/basic_tools/.envrc b/templates/basic_tools/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/templates/basic_tools/.envrc @@ -0,0 +1 @@ +use flake diff --git a/templates/basic_tools/flake.nix b/templates/basic_tools/flake.nix new file mode 100644 index 0000000..5e64afb --- /dev/null +++ b/templates/basic_tools/flake.nix @@ -0,0 +1,31 @@ +{ + description = "basic tool setup flake"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05"; + nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + flake-utils-plus.url = "github:gytis-ivaskevicius/flake-utils-plus"; + }; + + outputs = { self, nixpkgs, nixpkgs-unstable, flake-utils, flake-utils-plus, ... }@inputs: + { } // (flake-utils.lib.eachDefaultSystem (system: + let + unstable = import nixpkgs-unstable { + inherit system; + }; + pkgs = import nixpkgs { + inherit system; + }; + in + rec { + devShell = + pkgs.mkShell { + buildInputs = [ + # add packages here, like + # pkgs.clingo + ]; + }; + } + )); +} diff --git a/templates/jupyter/flake.nix b/templates/jupyter/flake.nix new file mode 100644 index 0000000..8234ce1 --- /dev/null +++ b/templates/jupyter/flake.nix @@ -0,0 +1,45 @@ +{ + description = "JupyterLab Flake"; + + inputs = { + jupyterWith.url = "github:tweag/jupyterWith"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, jupyterWith, flake-utils }: + flake-utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ] (system: + let + pkgs = import nixpkgs { + system = system; + overlays = nixpkgs.lib.attrValues jupyterWith.overlays; + }; + prince = pkgs.python3Packages.buildPythonPackage rec { + name = "prince"; + src = pkgs.fetchFromGitHub{ + owner = "MaxHalford"; + repo = "prince"; + rev = "bd5b29fafe853579c9d41e954caa4504d585665d"; + sha256 = "X7gpHvy2cfIKMrfSGLZxmJsytLbe/VZd27VsYIyEoTI="; + }; + propagatedBuildInputs = with pkgs.python3Packages; [ matplotlib pandas numpy scipy scikit-learn ]; + dontCheck = true; + dontUseSetuptoolsCheck = true; + }; + iPython = pkgs.kernels.iPythonWith { + name = "Python-env"; + packages = p: with p; [ sympy numpy pandas prince ]; + ignoreCollisions = true; + }; + jupyterEnvironment = pkgs.jupyterlabWith { + kernels = [ iPython ]; + }; + in rec { + apps.jupterlab = { + type = "app"; + program = "${jupyterEnvironment}/bin/jupyter-lab"; + }; + apps.default = apps.jupterlab; + devShell = jupyterEnvironment.env; + } + ); +} diff --git a/templates/rust/.envrc b/templates/rust/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/templates/rust/.envrc @@ -0,0 +1 @@ +use flake diff --git a/templates/rust/.gitignore b/templates/rust/.gitignore new file mode 100644 index 0000000..53a30ee --- /dev/null +++ b/templates/rust/.gitignore @@ -0,0 +1,22 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# Intellij project configuration +.idea + +# emacs tmp files +*~ +# Flycheck +flycheck_*.el + +# projectiles files +.projectile +/.direnv/ diff --git a/templates/rust/flake.nix b/templates/rust/flake.nix new file mode 100644 index 0000000..08193db --- /dev/null +++ b/templates/rust/flake.nix @@ -0,0 +1,53 @@ +{ + description = "basic rust flake"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05"; + nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; + flake-utils.url = "github:numtide/flake-utils"; + gitignoresrc = { + url = "github:hercules-ci/gitignore.nix"; + flake = false; + }; + }; + + outputs = { self, nixpkgs, nixpkgs-unstable, flake-utils, gitignoresrc, rust-overlay, ... }@inputs: + { + #overlay = import ./nix { inherit gitignoresrc; }; + } // (flake-utils.lib.eachDefaultSystem (system: + let + unstable = import nixpkgs-unstable { inherit system; }; + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay)]; + }; + in + rec { + devShell = + pkgs.mkShell { + RUST_LOG = "debug"; + RUST_BACKTRACE = 1; + buildInputs = [ + pkgs.rust-bin.stable.latest.rustfmt + pkgs.rust-bin.stable.latest.default + pkgs.rust-analyzer + pkgs.cargo-audit + pkgs.cargo-license + pkgs.cargo-tarpaulin + pkgs.cargo-kcov + pkgs.valgrind + pkgs.gnuplot + pkgs.kcov + ]; + }; + } + )); +} +