dotfiles/installers/install-nix-packages-flake.sh
2024-09-30 16:53:04 +00:00

125 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Installs packages to the nix profile using nix profile
# Reference: https://nixos.org/manual/nixpkgs/stable/#sec-declarative-package-management
set -ex
nix_packages=(
# Personal tools
"vimHugeX"
"ranger"
"jq"
"yq"
"ruby"
"shellcheck"
# Neovim + Plugin dependencies
"neovim"
"ripgrep"
"fzf"
# Language servers
"helm-ls" # Helm
"yaml-language-server" # Yaml
"lua-language-server" # Lua
"solargraph" # Ruby
# ProgrammingLanguages
"lua52Packages.lua"
"pass"
"gnupg"
"gnupg1"
"pinentry-curses"
# Professional tools
"skopeo"
"awscli2"
"kubectl"
"kubernetes-helm"
"rancher"
# Remote programming tools
"lemonade"
)
unstable_packages=(
# CVE in NixOS stable version of vault
"vault"
# Need unstable latest up to date terraform/terragrunt
"terraform"
"terragrunt"
# Language servers
"gitlab-ci-ls"
)
mkdir -p ~/.config/nixpkgs/
templated_insert="$(for nix_package in ${nix_packages[@]}; do echo " $nix_package"; echo; done)"
templated_insert=${templated_insert}$(for unstable_package in ${unstable_packages[@]}; do echo; echo " unstablePkgs.$unstable_package"; done)
# Reference: https://discourse.nixos.org/t/nix-profile-in-combination-with-declarative-package-management/21228/9
cat << EOF > ~/.config/nixpkgs/flake.nix
{
description = "A declarative system installation";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, unstable }:
let
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
# Generate a user-friendly version number.
version = builtins.substring 0 8 self.lastModifiedDate;
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; config.allowUnfree = true; });
in {
packages = forAllSystems (system:
let
pkgs = nixpkgsFor.\${system};
in {
default = self.packages.\${system}.myPackageCollection;
myPackageCollection = # libs and clis
let
pkgs = nixpkgs.legacyPackages.\${system}; # here we need just legacy packages
unstablePkgs = import unstable { inherit system; config.allowUnfree = true; };
in pkgs.buildEnv {
name = "myPackages";
paths = with pkgs; [
${templated_insert}
];
extraOutputsToInstall = [ "man" "doc" ];
};
}); # packages
}; # outputs
}
EOF
install_nix_packages()
{
# The name of the package we are going to install, needed to check for presence/uninstall
package_name=packages.x86_64-linux.myPackageCollection
if [ "${nix_packages[*]}" ]; then
if nix profile list | cut -d' ' -f 2 | grep -q "${package_name}"; then
echo "Removing previous version of profile"
nix profile remove "${package_name}"
fi
echo "Installing profile"
nix profile install "${HOME}/.config/nixpkgs/flake.nix#myPackageCollection"
fi
}
install_nix_packages