nix-configuration/modules/zsh/common/10-templates.zsh
2025-01-05 23:10:55 +01:00

125 lines
2.8 KiB
Bash

init-nix-flake() {
local tpl=$1
local name=$2
local flakePath="$(pwd)/flake.nix"
local envrcPath="$(pwd)/.envrc"
[[ -f "${flakePath}" ]] && echo "${flakePath} already present" && return 1
[[ -f "${envrcPath}" ]] && echo "${envrcPath} already present" && return 1
if [[ "$tpl" == "yp" ]]; then
cat <<EOF > "${flakePath}"
{
inputs = {
nix.url = "git+ssh://git@bitbucket.org/yourpass/nix";
};
outputs = { self, nix }: {
formatter = nix.formatter;
devShells = nix.lib.forAllSystems (pkgs: {
default = pkgs.devshell.mkShell {
name = "yp-${name}";
packages = with pkgs; [
];
commands = [];
};
});
};
}
EOF
echo "use flake" > "${envrcPath}"
direnv allow
elif [[ "$tpl" == "investbay" ]]; then
cat <<EOF > "${flakePath}"
{
inputs = {
nix.url = "git+ssh://git@git.investbay.dev/morosystems/investbay/devops/nix?branch=main";
};
outputs = { self, nix }: {
formatter = nix.formatter;
devShells = nix.lib.forAllSystems (pkgs: {
default = pkgs.devshell.mkShell {
name = "investbay-${name}";
packages = with pkgs; [
];
commands = [];
};
});
};
}
EOF
echo "use flake" > "${envrcPath}"
direnv allow
elif [[ "$tpl" == "c3c" ]]; then
cat <<EOF > "${flakePath}"
{
inputs = {
nix.url = "git+ssh://git@git.c3c.cz/C3C/nix";
};
outputs = { self, nix }: {
formatter = nix.formatter;
devShells = nix.lib.forAllSystems (pkgs: {
default = nix.lib.mkDevenvShell {
inherit pkgs;
inputs = {
self = self;
nixpkgs = pkgs;
};
modules = [
{
packages = with pkgs; [
];
scripts = {
menu = {
description = "Print this menu";
exec = ''
echo "Commands:"
echo -n '\${
builtins.toJSON (
builtins.mapAttrs (s: value: value.description) self.devShells.\${pkgs.system}.default.config.scripts
)
}' | \
\${pkgs.jq}/bin/jq -r 'to_entries | map(" \(.key)\n" + " - \(if .value == "" then "no description provided" else .value end)") | "" + .[]'
'';
};
fix = {
exec = ''
\${nix.lib.cd_root}
nix fmt ./*.nix
\${pkgs.golangci-lint}/bin/golangci-lint run --sort-results --out-format tab --config \${nix.lib.golangci-config-file} --fix --issues-exit-code 0 ./...
stylua ./src
'';
};
};
}
];
};
});
};
}
EOF
echo "use flake . --impure" > "${envrcPath}"
direnv allow
else
echo "Wrong template as first arg: [yp/investbay/c3c]"
return 1
fi
}