From 96411aaa21543c81ce53fd9210e63c5e3d4ac519 Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 22 Aug 2022 18:31:14 +0200 Subject: bootstrap lib, add mkDevShell --- flake.nix | 9 +++++++ lib/mk-dev-shell.nix | 30 +++++++++++++++++++++++ readme.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 flake.nix create mode 100644 lib/mk-dev-shell.nix create mode 100644 readme.md diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..57a6896 --- /dev/null +++ b/flake.nix @@ -0,0 +1,9 @@ +{ + description = "Additional flake utils for convenience."; + + outputs = { self }: { + lib = { + mkDevShell = import ./lib/mk-dev-shell.nix; + }; + }; +} diff --git a/lib/mk-dev-shell.nix b/lib/mk-dev-shell.nix new file mode 100644 index 0000000..e0bb441 --- /dev/null +++ b/lib/mk-dev-shell.nix @@ -0,0 +1,30 @@ +{ pkgs +, tools ? [] +, envVars ? {} +}: + +let + # TODO: escape values properly + exportEnvVar = k: v: ''export ${k}="${v}"; echo ${k}=\"${v}\"''; + exportedEnvVars = pkgs.lib.mapAttrsToList exportEnvVar envVars; + +in pkgs.mkShell { + buildInputs = tools; + + # TODO: handle case with no env variable set + # TODO: handle case with no package in tools + # TODO: allow custom shell + shellHook = '' + echo -e "\nDEVSHELL ENVIRONMENT VARIABLES:" + ${pkgs.lib.concatStringsSep "\n" exportedEnvVars} + + echo -e "\nDEVSHELL COMMANDS:" + ls "${pkgs.symlinkJoin { name = "env"; paths = tools; }}/bin" + + # Use the default user shell instead of Bash + $(${pkgs.finger_bsd}/bin/finger $USER \ + | ${pkgs.gnugrep}/bin/grep -oP 'Shell: \K.*') + + exit $? + ''; +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..88c4d4a --- /dev/null +++ b/readme.md @@ -0,0 +1,68 @@ +# flaky-utils + +Additional Nix Flake utility functions for personal convenience (mainly +reducing boilerplate). + +No promise on API stability. + + +## Usage + +Functions documentation provided as comments below. + +```nix +{ + description = "Example of a Flake using flaky-utils."; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05"; + flake-utils.url = "github:numtide/flake-utils"; + flaky-utils.url = "git+https://cgit.pacien.net/libs/flaky-utils"; + }; + + outputs = { self, nixpkgs, flake-utils, flaky-utils }: + flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs { inherit system; }; + in { + + # Convenience development shell providing some tools. + # + # The binaries made available and the environment variable set are printed + # when entering the shell. + # + # The user's default shell is used instead of Bash (sacrifying a bit of + # reproducibility for convenience). + # + devShell = flaky-utils.lib.mkDevShell { + inherit pkgs; + + tools = with pkgs; [ + postgresql_14 + pgcli + ]; + + envVars = rec { + PGDATA = "$PWD/development_database/pgdata"; + PGHOST = "$PWD/development_database"; + PGPORT = "5432"; + PGDATABASE = "app"; + DATABASE_URL = "postgresql:///${PGDATABASE}?host=${PGHOST}"; + }; + }; + + }); +} +``` + + +## Contributing + +Issues and patches: email the author. + + +## Licence and copyright + +Copyright (C) 2022 Pacien TRAN-GIRARD. + +This project is distributed under the terms of European Union Public Licence +version 1.2, a copy of which is provided in `./licence.txt`. -- cgit v1.2.3