197 lines
5.0 KiB
Bash
Executable File
197 lines
5.0 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 -e
|
|
|
|
FLAKE_PATH="${HOME}/.config/nix/flakes/my-package-collection"
|
|
FLAKE_NAME="flake.nix"
|
|
ARCHIVE_PATH="${FLAKE_PATH}/archive.nar"
|
|
|
|
if ! which nix &>/dev/null; then
|
|
if [ -r "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
|
|
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
|
|
else
|
|
echo "Nix isn't available. Exiting now!"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
nix_packages=(
|
|
# Personal tools
|
|
"ranger"
|
|
"jq"
|
|
"yq"
|
|
"screen"
|
|
"xdg-utils" # For xdg-open
|
|
"zk"
|
|
|
|
# Neovim + Plugin dependencies
|
|
"neovim"
|
|
"ripgrep"
|
|
"fzf"
|
|
|
|
# Language servers
|
|
"helm-ls" # Helm
|
|
"yaml-language-server" # Yaml
|
|
"lua-language-server" # Lua
|
|
"solargraph" # Ruby
|
|
# Terraform
|
|
"terraform-ls"
|
|
"tflint"
|
|
"python3Packages.python-lsp-server" # Python
|
|
"shellcheck"
|
|
|
|
# ProgrammingLanguages
|
|
"lua52Packages.lua"
|
|
"ruby"
|
|
"python3"
|
|
|
|
"pass"
|
|
"gnupg"
|
|
"gnupg1"
|
|
"pinentry-curses"
|
|
|
|
# Professional tools
|
|
"skopeo"
|
|
"awscli2"
|
|
|
|
"kubectl"
|
|
"kubernetes-helm"
|
|
"rancher"
|
|
"gitlab-ci-local"
|
|
|
|
"distrobox"
|
|
|
|
# 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"
|
|
)
|
|
|
|
|
|
generate_flake() {
|
|
mkdir -p "${FLAKE_PATH}"
|
|
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 > "${FLAKE_PATH}/${FLAKE_NAME}"
|
|
{
|
|
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 = "myPackageCollection";
|
|
paths = with pkgs; [
|
|
${templated_insert}
|
|
];
|
|
|
|
extraOutputsToInstall = [ "man" "doc" ];
|
|
};
|
|
}); # packages
|
|
}; # outputs
|
|
}
|
|
EOF
|
|
}
|
|
|
|
lock_flake() {
|
|
nix flake lock "${FLAKE_PATH}"
|
|
}
|
|
|
|
flake_lock_exists() {
|
|
if [ -r "${FLAKE_PATH}/flake.lock" ]; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
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
|
|
# Updated to process new nix profile format, proper processing probably needs JQ
|
|
# But JQ would be another dependency and this is good enough for now
|
|
# Okay we are installing jq to do stuff now but I don't feel like reworking this
|
|
if nix profile list --json | grep -qi "\"attrPath\":\"${package_name}\""; then
|
|
echo "Removing previous version of profile"
|
|
store_path=$(nix profile list --json | nix-shell --quiet -p jq --run 'jq -r .elements.myPackageCollection.storePaths[0]')
|
|
nix profile remove "${store_path}"
|
|
fi
|
|
echo "Installing profile"
|
|
nix profile install "${FLAKE_PATH}/#myPackageCollection"
|
|
fi
|
|
}
|
|
|
|
get_store_path() {
|
|
# This is only called post successful installation so jq is available
|
|
nix profile list --json | jq -r .elements.myPackageCollection.storePaths[0]
|
|
}
|
|
|
|
export_archive() {
|
|
store_path="${1}"
|
|
mkdir -p "$(dirname ${ARCHIVE_PATH})"
|
|
nix-store --export $(nix-store --query --requisites ${store_path}) > "${ARCHIVE_PATH}"
|
|
}
|
|
|
|
import_archive() {
|
|
nix-store --import < "${ARCHIVE_PATH}" > /dev/null
|
|
}
|
|
|
|
archive_exists() {
|
|
if [ -r "${ARCHIVE_PATH}" ]; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
if archive_exists; then
|
|
import_archive
|
|
fi
|
|
generate_flake
|
|
if ! flake_lock_exists; then
|
|
lock_flake
|
|
fi
|
|
install_nix_packages
|
|
if ! archive_exists; then
|
|
export_archive "$(get_store_path)"
|
|
fi
|