Refactor nix init scripts

This commit is contained in:
Lukas Cech 2025-01-23 13:03:52 +01:00
parent 28a44e36fa
commit 46f71fabdd
4 changed files with 95 additions and 69 deletions

View File

@ -229,7 +229,7 @@ in
character = { character = {
success_symbol = "[](bold green)"; success_symbol = "[](bold green)";
error_symbol = "[](bold red)"; error_symbol = "[!](bold red)";
}; };
}; };
}; };

View File

@ -3,19 +3,21 @@
nix.url = "git+ssh://git@git.investbay.dev/devops/nix?branch=main"; nix.url = "git+ssh://git@git.investbay.dev/devops/nix?branch=main";
}; };
outputs = { self, nix }: { outputs =
formatter = nix.formatter; { self, nix }:
{
formatter = nix.formatter;
devShells = nix.lib.forAllSystems (pkgs: { devShells = nix.lib.forAllSystems (pkgs: {
default = pkgs.devshell.mkShell { default = pkgs.devshell.mkShell {
name = "investbay"; name = "investbay";
packages = with pkgs; [ packages = with pkgs; [
]; ];
commands = []; commands = [ ];
}; };
}); });
}; };
} }

View File

@ -3,19 +3,21 @@
nix.url = "git+ssh://git@bitbucket.org/yourpass/nix"; nix.url = "git+ssh://git@bitbucket.org/yourpass/nix";
}; };
outputs = { self, nix }: { outputs =
formatter = nix.formatter; { self, nix }:
{
formatter = nix.formatter;
devShells = nix.lib.forAllSystems (pkgs: { devShells = nix.lib.forAllSystems (pkgs: {
default = pkgs.devshell.mkShell { default = pkgs.devshell.mkShell {
name = "yp"; name = "yp";
packages = with pkgs; [ packages = with pkgs; [
]; ];
commands = []; commands = [ ];
}; };
}); });
}; };
} }

View File

@ -1,68 +1,90 @@
{ lib, pkgs, ... }: { lib, pkgs, ... }:
let let
flake-dir = {flake, impure ? false}: pkgs.writeShellScript "nix-prepare" '' flake-dir =
run() { {
local dir="$1" flake,
echo "Preparing nix environment in $dir" impure ? false,
}:
pkgs.writeShellScript "nix-prepare" ''
run() {
local dir="$1"
echo "Preparing nix environment in $dir"
if [ "$dir" = "" ]; then if [ "$dir" = "" ]; then
echo "Usage: nix-prepare <dir>" echo "Usage: nix-prepare <dir>"
exit 1 exit 1
fi fi
mkdir -p $dir mkdir -p $dir
cd $dir cd $dir
git init git init
# Insert the nix flake # Insert the nix flake
cp ${flake} flake.nix cp ${flake} flake.nix
chmod 644 flake.nix chmod 644 flake.nix
echo '/*' > .gitignore echo '/*' > .gitignore
echo '!/.gitignore' >> .gitignore echo '!/.gitignore' >> .gitignore
echo '!/flake.nix' >> .gitignore echo '!/flake.nix' >> .gitignore
echo '!/flake.lock' >> .gitignore echo '!/flake.lock' >> .gitignore
echo 'use flake .${(if impure then " --impure" else "")}' > .envrc echo 'use flake .${(if impure then " --impure" else "")}' > .envrc
git add flake.nix .gitignore git add flake.nix .gitignore
git commit -m "Setup initial flake" git commit -m "Setup initial flake"
direnv allow direnv allow
eval "$(direnv export bash)" eval "$(direnv export bash)"
git add flake.lock git add flake.lock
git commit -m "Lock flakes" git commit -m "Lock flakes"
} }
run "$@" run "$@"
''; '';
git-flake-dir-clone = { flake, impure? false}: pkgs.writeShellScript "git-nix-clone" '' git-flake-dir-clone =
# Input is a git repository such as git@github.com:group/project-name.git {
run() { flake,
local repo="$1" impure ? false,
local projectName=$(echo "$repo" | sed 's/.*\///' | sed 's/\.git//') }:
pkgs.writeShellScript "git-nix-clone" ''
# Input is a git repository such as git@github.com:group/project-name.git
run() {
local repo="$1"
local projectName=$(echo "$repo" | sed 's/.*\///' | sed 's/\.git//')
if [ "$repo" = "" ]; then if [ "$repo" = "" ]; then
echo "Usage: git-nix-clone <repo>" echo "Usage: git-nix-clone <repo>"
exit 1 exit 1
fi fi
${flake-dir {inherit flake impure;}} "$projectName"-nix ${flake-dir { inherit flake impure; }} "$projectName"-nix
git clone "$repo" "$projectName"-nix"/$projectName" git clone "$repo" "$projectName"-nix"/$projectName"
} }
run "$@" run "$@"
''; '';
in in
{ {
programs.zsh.shellAliases = { programs.zsh.shellAliases = {
git-c3c-clone = lib.mkDefault "${git-flake-dir-clone { flake = ./flake-templates/c3c-flake.nix; impure = false;}}"; git-c3c-clone = lib.mkDefault "${git-flake-dir-clone {
flake-dir-c3c = lib.mkDefault "${flake-dir { flake = ./flake-templates/c3c-flake.nix; impure = false;}}"; flake = ./flake-templates/c3c-flake.nix;
flake-dir-yp = lib.mkDefault "${flake-dir { flake = ./flake-templates/yp-flake.nix; impure = false;}}"; impure = true;
flake-dir-investbay = lib.mkDefault "${flake-dir { flake = ./flake-templates/investbay-flake.nix; impure = false;}}"; }}";
flake-dir-c3c = lib.mkDefault "${flake-dir {
flake = ./flake-templates/c3c-flake.nix;
impure = true;
}}";
flake-dir-yp = lib.mkDefault "${flake-dir {
flake = ./flake-templates/yp-flake.nix;
impure = false;
}}";
flake-dir-investbay = lib.mkDefault "${flake-dir {
flake = ./flake-templates/investbay-flake.nix;
impure = false;
}}";
}; };
} }