From e68ccd8bc701e4622b323144a7a50bdbe82662ca Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 01/10] Add a vim option select --- bash/.aliases | 3 +-- bash/.functions | 10 ++++++++++ install.sh | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bash/.aliases b/bash/.aliases index 71ca0be..c1b6a99 100644 --- a/bash/.aliases +++ b/bash/.aliases @@ -1,7 +1,5 @@ #{- ~/.aliases -}# -alias vim="nvim " - #{- FILE OPERATIONS -}# # Allow alias expansion for commands executed with sudo @@ -40,6 +38,7 @@ alias df="df -h" alias du="du -sh" # History searches +alias phistory='cat ${PERSISTENT_HISTFILE}' alias hgrep='history | grep --color' alias phgrep="cat ${PERSISTENT_HISTFILE} | grep --color" diff --git a/bash/.functions b/bash/.functions index 41c5043..c412f00 100644 --- a/bash/.functions +++ b/bash/.functions @@ -21,3 +21,13 @@ cat_with_newline() cat "$@" && echo } alias cat="cat_with_newline" + +vim_option_select() +{ + if which nvim &> /dev/null; then + nvim "$@" + else + vim "$@" + fi +} +alias vim="vim_option_select " diff --git a/install.sh b/install.sh index d4d8483..15c7aae 100755 --- a/install.sh +++ b/install.sh @@ -7,8 +7,8 @@ if [ -f /etc/os-release ]; then echo "Idenitfying OS to install GNU stow..." if grep -q -i debian /etc/os-release; then echo "You've got Debian!"; - sudo apt update; - sudo apt install -y stow; + sudo apt-get update; + sudo apt-get install -y stow; elif grep -q -i rhel /etc/os-release || grep -q -i fedora /etc/os-release; then echo "You've got RedHat (or maybe Fedora)! Which is unsupported right now sorry!"; # sudo yum install -y stow; From e332049a0d6c43031bb6b7f2d7a5dbab0ca7a7ce Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 02/10] Attempt to fix recursive cloning for submodules --- install.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 15c7aae..978a71c 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,11 @@ #!/usr/bin/env bash +# Install my dotfiles on a variety of systems -echo "CURRENT LOCATION: $PWD" +set -ex + +echo "PWD: $PWD" +# Coder dotfiles (and most other ways of doing this) will not do a recursive clone, so do that in install script. +git submodule init && git submodule update echo "Installing dotfiles..." if [ -f /etc/os-release ]; then @@ -28,6 +33,6 @@ else fi # Apply the dotfiles with stow -echo "RUNNING APPLY DOTFILES" +echo "Running apply dotfiles" ./apply-dotfiles -echo "RAN APPLY DOTFILES" +echo "Dotfiles applied successfully" From e931cb3c81b383366ebf59ffcd83b21323a28b31 Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 03/10] Option select on tput working or use old fallback control codes --- bash/.bashrc | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/bash/.bashrc b/bash/.bashrc index 17f9e4f..110982c 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -2,13 +2,23 @@ #{- PROMPT -}# -red="\001$(tput bold)$(tput setaf 1)\002" -green="\001$(tput bold)$(tput setaf 2)\002" -yellow="\001$(tput bold)$(tput setaf 3)\002" -blue="\001$(tput bold)$(tput setaf 4)\002" -magenta="\001$(tput bold)$(tput setaf 5)\002" -cyan="\001$(tput bold)$(tput setaf 6)\002" -no_color="\001$(tput sgr0)\002" +if which tput &> /dev/null && tput bold &> /dev/null; then + red="\001$(tput bold)$(tput setaf 1)\002" + green="\001$(tput bold)$(tput setaf 2)\002" + yellow="\001$(tput bold)$(tput setaf 3)\002" + blue="\001$(tput bold)$(tput setaf 4)\002" + magenta="\001$(tput bold)$(tput setaf 5)\002" + cyan="\001$(tput bold)$(tput setaf 6)\002" + no_color="\001$(tput sgr0)\002" +else + red="\[\033[1;31m\]" + green="\[\033[1;32m\]" + yellow="\[\033[1;33m\]" + blue="\[\033[1;34m\]" + magenta="\[\033[1;35m\]" + cyan="\[\033[1;36m\]" + no_color="\[\033[0m\]" +fi export PS1="${green}\$${no_color} " From bd497a668dcbea9a9556c30dc73965a462ba3012 Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 04/10] Beginner luasnip configuration for terraform --- vim/.config/nvim/lua/user/luasnip.lua | 68 +++++++++++++++++---------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/vim/.config/nvim/lua/user/luasnip.lua b/vim/.config/nvim/lua/user/luasnip.lua index a151dd7..3650e95 100644 --- a/vim/.config/nvim/lua/user/luasnip.lua +++ b/vim/.config/nvim/lua/user/luasnip.lua @@ -34,7 +34,8 @@ ls.config.set_config { -- Expand if expandable, or jump forward if jumpable -- If neither, do nothing queitly -vim.keymap.set({ "i", "s" }, "", function() +-- vim.keymap.set({ "i", "s" }, "", function() +vim.keymap.set({ "i", "s" }, "", function() if ls.expand_or_jumpable() then ls.expand_or_jump() end @@ -42,41 +43,56 @@ end, { silent = true }) -- end, { silent = true }) -- Jump backwards to the previous jumpable point -vim.keymap.set({ "i", "s" }, "", function() +-- vim.keymap.set({ "i", "s" }, "", function() +vim.keymap.set({ "i", "s" }, "", function() if ls.jumpable(-1) then ls.jump(-1) end end, { silent = true }) -- Selecting from a list of options in a choice node -vim.keymap.set({ "i" }, "", function() +-- vim.keymap.set({ "i" }, "", function() +vim.keymap.set({ "i" }, "", function() if ls.choice_active() then ls.change_choice(1) end end) --- Allow us to hot reload snippets -vim.keymap.set("n", "s", "source ~/.config/nvim/lua/user/luasnip.lua") +-- Allow us to hot reload snippets by unloading snippets then resourcing file +local ls_cleanup = function() + ls.cleanup() + vim.cmd("source ~/.config/nvim/lua/user/luasnip.lua") +end +vim.keymap.set("n", "s", ls_cleanup) -ls.add_snippets('lua', { - ls.parser.parse_snippet("expand", "-- this is what was expanded!"), - ls.parser.parse_snippet("lf", "local $1 = function($2)\n $0\nend"), - ls.parser.parse_snippet("mf", "$1.$2 = function($3)\n $0\nend"), - -- Example from snippet writing guide: https://github.com/L3MON4D3/LuaSnip/blob/master/Examples/snippets.lua#L190 - s( - "fmt1", - fmt("To {title} {} {}.", { - i(2, "Name"), - i(3, "Surname"), - title = c(1, { t("Mr."), t("Ms.") }), - }) - ), - -- Example from video: https://www.youtube.com/watch?v=Dn800rlPIho - s( - "req", - fmt("local {} = require('{}')", { - i(1, "default"), - rep(1) - }) - ), +ls.add_snippets(nil, { + terraform = { + ls.parser.parse_snippet("res", "resource \"$1\" \"$2\" {\n $3\n}"), + ls.parser.parse_snippet("dat", "data \"$1\" \"$2\" {\n $3\n}"), + ls.parser.parse_snippet("var", "variable \"$1\" {\n type = $2\n description = $3\n}"), + ls.parser.parse_snippet("loc", "locals {\n $1\n}"), + ls.parser.parse_snippet("mod", "module {\n source = $1\n}"), + }, + lua = { + ls.parser.parse_snippet("expand", "-- this is what was expanded!"), + ls.parser.parse_snippet("lf", "local $1 = function($2)\n $0\nend"), + ls.parser.parse_snippet("mf", "$1.$2 = function($3)\n $0\nend"), + -- Example from snippet writing guide: https://github.com/L3MON4D3/LuaSnip/blob/master/Examples/snippets.lua#L190 + s( + "fmt1", + fmt("To {title} {} {}.", { + i(2, "Name"), + i(3, "Surname"), + title = c(1, { t("Mr."), t("Ms.") }), + }) + ), + -- Example from video: https://www.youtube.com/watch?v=Dn800rlPIho + s( + "req", + fmt("local {} = require('{}')", { + i(1, "default"), + rep(1) + }) + ), + } }) From 6d2071b1ea5c30c49b5956298aa2fc70cb77d000 Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 05/10] Fix Direnv prompt whenver off primary hosts --- bash/.bashrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash/.bashrc b/bash/.bashrc index 110982c..102bd56 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -24,8 +24,8 @@ fi export PS1="${green}\$${no_color} " export DIRENV_PS1="${red}\$${no_color} " if [ "${HOSTNAME}" != "crypt" ] && [ "${HOSTNAME}" != "tomb" ] || [ "${SSH_TTY}" != "" ]; then + DIRENV_PS1="${DIRENV_PS1}${blue}${HOSTNAME} ${DIRENV_PS1}" PS1="${PS1}${blue}${HOSTNAME} ${PS1}" - DIRENV_PS1="${PS1}${blue}${HOSTNAME} ${PS1}" fi export ORIG_PS1=${PS1} From 247417af95b824b16e9f4fc05dfe193f18b3283e Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 06/10] Make direnv prompt updating conditional to direnv's presence --- bash/.bashrc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/bash/.bashrc b/bash/.bashrc index 102bd56..ddabf3b 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -29,14 +29,16 @@ if [ "${HOSTNAME}" != "crypt" ] && [ "${HOSTNAME}" != "tomb" ] || [ "${SSH_TTY}" fi export ORIG_PS1=${PS1} -update_prompt() { - if direnv status | grep -iq 'RC allowed true'; then - PS1=${DIRENV_PS1} - else - PS1=${ORIG_PS1} - fi -} -PROMPT_COMMAND="update_prompt; $PROMPT_COMMAND" +if which direnv &> /dev/null; then + update_prompt() { + if direnv status | grep -iq 'RC allowed true'; then + PS1=${DIRENV_PS1} + else + PS1=${ORIG_PS1} + fi + } + PROMPT_COMMAND="update_prompt; $PROMPT_COMMAND" +fi # Continuation Prompt: "> " in cyan export PS2="${cyan}>${no_color} " From d5b8675fef1528bfb1248a19567b2683b40057e3 Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 07/10] 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 From 4385fa7c0169165dafa9621b87e75ccc1cc51d6b Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 08/10] Remove vestigial vagrant reference --- installers/install-nix.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/installers/install-nix.sh b/installers/install-nix.sh index 766f703..2c03293 100755 --- a/installers/install-nix.sh +++ b/installers/install-nix.sh @@ -18,7 +18,7 @@ curl -so install-nix https://releases.nixos.org/nix/nix-2.18.1/install sh ./install-nix # Source the nix profile -cat << EOF > ~vagrant/.bashrc +cat << EOF > ~/.bashrc # Source nix if it's installed if [ -r ~/.nix-profile/etc/profile.d/nix.sh ]; then . ~/.nix-profile/etc/profile.d/nix.sh From 6565a041e10714ce1315a9e1d63c408f1a5ec06c Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 09/10] Automatically install coder in nix environments --- install.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index 978a71c..d306db7 100755 --- a/install.sh +++ b/install.sh @@ -7,13 +7,17 @@ echo "PWD: $PWD" # Coder dotfiles (and most other ways of doing this) will not do a recursive clone, so do that in install script. git submodule init && git submodule update +# Match case insensitively to make things easier for this part +shopt -s nocasematch + echo "Installing dotfiles..." if [ -f /etc/os-release ]; then echo "Idenitfying OS to install GNU stow..." - if grep -q -i debian /etc/os-release; then - echo "You've got Debian!"; + if [ "${CODER_OS_FAMILY:-}" == "Ubuntu" ] || grep -q -i debian /etc/os-release; then + echo "Detected OS as Ubuntu, using apt"; sudo apt-get update; sudo apt-get install -y stow; + installers/install-nix.sh elif grep -q -i rhel /etc/os-release || grep -q -i fedora /etc/os-release; then echo "You've got RedHat (or maybe Fedora)! Which is unsupported right now sorry!"; # sudo yum install -y stow; @@ -28,9 +32,13 @@ if [ -f /etc/os-release ]; then exit 1; fi; else + if [ "${CODER_TEMPLATE_TYPE}" == "Devcontainer" ]; then + installers/install-nix.sh + fi echo "ERROR: Unable to find '/etc/os-release' information!" 1>&2; exit 1; fi +shopt -u nocasematch # Apply the dotfiles with stow echo "Running apply dotfiles" From 1d6d53e58077fa04d5c1db109ac704eb58d732a6 Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH 10/10] Make a root single user nix install possible Taken from this issue https://github.com/NixOS/nix/issues/1559 This is only so that the script will succeed when running in docker containers without systemd (can't use multi-user) and that have root as the default user, a shockingly common practice --- installers/install-nix.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/installers/install-nix.sh b/installers/install-nix.sh index 2c03293..405975f 100755 --- a/installers/install-nix.sh +++ b/installers/install-nix.sh @@ -11,6 +11,12 @@ if [ -d /nix ]; then exit 0 fi +# Workaround to make a single user install work as root +if [ "${USER:-}" == "root" ] || [ "${UID:-}" == "0" ]; then + mkdir -p /etc/nix + echo "build-users-group =" > /etc/nix/nix.conf +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