From 781b1c026fd52077adaa324c7ccec494e0f6c70f Mon Sep 17 00:00:00 2001 From: anmiller Date: Fri, 17 May 2024 15:27:49 +0000 Subject: [PATCH] Add installers for common programs to bring along --- installers/install-nix-packages-flake.sh | 109 +++++++++++++++++++++++ installers/install-nix.sh | 35 ++++++++ 2 files changed, 144 insertions(+) create mode 100755 installers/install-nix-packages-flake.sh create mode 100755 installers/install-nix.sh diff --git a/installers/install-nix-packages-flake.sh b/installers/install-nix-packages-flake.sh new file mode 100755 index 0000000..4c7712f --- /dev/null +++ b/installers/install-nix-packages-flake.sh @@ -0,0 +1,109 @@ +#!/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" + + # Neovim + Plugin dependencies + "neovim" + "ripgrep" + "fzf" + + # Language servers + "helm-ls" + "yaml-language-server" + "lua-language-server" + + # ProgrammingLanguages + "lua52Packages.lua" + + "pass" + "gnupg" + + # Professional tools + "skopeo" + "awscli2" + + "kubectl" + "kubernetes-helm" + "rancher" + + #"terraform" + "opentofu" + "terragrunt" +) +unstable_packages=( + # CVE in NixOS stable version of vault + "vault" +) + + +mkdir -p ~/.config/nixpkgs/ +templated_insert=$(for nix_package in ${nix_packages[@]}; do echo " $nix_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-23.11"; # also possible: nixos-unstable + }; + + outputs = { self, nixpkgs }: + 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; }); + 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 + 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 diff --git a/installers/install-nix.sh b/installers/install-nix.sh new file mode 100755 index 0000000..766f703 --- /dev/null +++ b/installers/install-nix.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +# Install the Nix package manager as a secondary package manager. +# This can sometimes be useful for getting more recent packages. + +set -e +set -x + +# Abort installation if Nix is already installed +if [ -d /nix ]; then + exit 0 +fi + +# Download the Nix installer and it's signature from NixOS.org +curl -so install-nix https://releases.nixos.org/nix/nix-2.18.1/install + +# Run the installer +sh ./install-nix + +# Source the nix profile +cat << EOF > ~vagrant/.bashrc +# Source nix if it's installed +if [ -r ~/.nix-profile/etc/profile.d/nix.sh ]; then + . ~/.nix-profile/etc/profile.d/nix.sh +fi +EOF + +# Remove the installer and signature +rm -f ./install-nix + +# Enable flakes and the nix command +mkdir -p ~/.config/nix +cat < ~/.config/nix/nix.conf +experimental-features = flakes nix-command +EOF