From dbcba2ea2b7301aaa7e6487c2589b09ef09ba066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 24 May 2019 14:43:09 +0200 Subject: Reorganize files --- Makefile | 20 +++++ default.nix | 2 +- fetch_version | 173 ----------------------------------------- lib/default.nix | 82 +++++++++++++++++++ libs.nix | 82 ------------------- nix_path_env | 17 ---- nixops/default.nix | 9 +++ nixops/eldiron.nix | 9 --- nixops/scripts/nixops_wrap | 2 +- nixops/scripts/pull_deployment | 2 +- nixops/scripts/push_deployment | 2 +- nixops/scripts/setup | 6 +- overlays/default.nix | 2 +- pkgs/default.nix | 2 +- pkgs/private/default.nix | 2 +- scripts/fetch_version | 173 +++++++++++++++++++++++++++++++++++++++++ scripts/make-env | 7 +- scripts/nix_env | 17 ++++ scripts/nix_infos | 2 +- 19 files changed, 316 insertions(+), 295 deletions(-) create mode 100644 Makefile delete mode 100755 fetch_version create mode 100644 lib/default.nix delete mode 100644 libs.nix delete mode 100644 nix_path_env create mode 100644 nixops/default.nix delete mode 100644 nixops/eldiron.nix create mode 100755 scripts/fetch_version create mode 100644 scripts/nix_env diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f4bb3f5 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +subrecipes = setup ssh-eldiron info debug dry-run build upload deploy reboot push pull pull-deployment list-generations delete-generations cleanup +${subrecipes}: + @$(MAKE) --no-print-directory -C nixops/ $@ + +env: + ./scripts/make-env + +env-dry-run: + ./scripts/make-env --dry-run + +nix-info: + NIXOPS_DEPLOYMENT="" ./scripts/nix_infos + +nix-info-nixops: + NIXOPS_DEPLOYMENT="n" ./scripts/nix_infos + +nur: + ./scripts/make-nur + +.PHONY: env env-dry-run nix-info nix-info-nixops diff --git a/default.nix b/default.nix index c7f515b..ae27e32 100644 --- a/default.nix +++ b/default.nix @@ -4,7 +4,7 @@ let mypkgs = import ./pkgs { pkgs = pkgs_; }; in { - lib = import ./libs.nix { pkgs = pkgs_; }; + lib = import ./lib { pkgs = pkgs_; }; modules = import ./modules; overlays = import ./overlays; pkgs = mypkgs; diff --git a/fetch_version b/fetch_version deleted file mode 100755 index a0325f6..0000000 --- a/fetch_version +++ /dev/null @@ -1,173 +0,0 @@ -#!/bin/bash - -usage() { - echo "$0 file.json" - echo "$0 [-n|--name name] [-b|--branch branch_or_rev] [-f|--file out_file] [-h|--help] (-u|--url) url" - exit -} - -branch="master" -while [[ $# -gt 0 ]]; do - a="$1" - shift - case "$a" in - *.json) - file=$a - content=$(cat $a) - name="$(echo "$content" | jq -r ".meta.name")" - url="$(echo "$content" | jq -r ".meta.url")" - branch="$(echo "$content" | jq -r ".meta.branch")" - ;; - -n|--name) - name=$1 - shift - ;; - -u|--url) - url=$1 - shift - ;; - -b|--branch) - branch=$1 - shift - ;; - -f|--file) - file=$1 - shift - ;; - -h|--help) - usage - ;; - esac -done -if [ -z "$url" ]; then - usage -fi -if [ -z "$name" ]; then - name=$(echo "$url" | cut -d"/" -f5) -fi -if [ -z "$file" ]; then - file=$name.json -fi - -# function fetch_ledger () { -# pushd $HOME/projets/ledger >/dev/null 2>/dev/null -# git fetch origin -# tag="$(git describe origin/next | sed -e "s/^v//")" -# rev="$(git show-ref -s refs/remotes/origin/next)" -# sha="$(nix-prefetch-url --unpack file://<(git archive --format=tar.gz HEAD) 2>/dev/null)" -# popd >/dev/null 2>/dev/null -# } - -# awk_describe='BEGIN { -# FS = "[ /^]+" -# while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) { -# if (!sha) -# sha = substr($0, 1, 7) -# tag = $3 -# } -# while ("curl -s " ARGV[1] "/releases/tag/" tag | getline) -# if ($3 ~ "commits") -# com = $2 -# printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha -# }' - -function get_ref () { - case "$1" in - refs/*) - echo "$1" - ;; - *) - echo "refs/heads/$1" - ;; - esac -} - -function get_name () { - branch="$1" - rev="$2" - minirev=${rev:0:7} - - case "$branch" in - refs/tags/*) - b="${branch#refs/tags/}" - echo "${b//\//-}" - ;; - refs/heads/*) - b=${branch#refs/heads/} - echo "$minirev-${b//\//-}" - ;; - refs/*) - b=${branch#refs/} - echo "$minirev-${b//\//-}" - ;; - *) - echo "$minirev-${branch//\//-}" - ;; - esac -} - -function fetch_github () { - rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)" - sha="$(nix-prefetch-url --unpack $url/archive/$rev.tar.gz)" - # Différent du git-describe et github-spécifique - #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//") - tag=$(get_name $branch $rev) -} - -function fetch_other () { - rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)" - sha="$(nix-prefetch-git --url $url --rev $(get_ref $branch) | jq -r '.sha256')" - tag=$(get_name $branch $rev) -} - -case "$url" in - https://*github.com/*) - fetch_github 2>/dev/null - owner=$(echo "$url" | cut -d"/" -f4) - repo=$(echo "$url" | cut -d"/" -f5) - - F='{ - "tag": $tag, - "meta": { - "name": $name, - "url": $url, - "branch": $branch - }, - "github": { - "owner": $owner, - "repo": $repo, - "rev": $rev, - "sha256": $sha, - "fetchSubmodules": true - } - }' - ;; - *) - fetch_other 2>/dev/null - F='{ - "tag": $tag, - "meta": { - "name": $name, - "url": $url, - "branch": $branch - }, - "git": { - "url": $url, - "rev": $rev, - "sha256": $sha, - "fetchSubmodules": true - } - }' - ;; -esac - -jq -n \ - --arg name "$name" \ - --arg owner "$owner" \ - --arg repo "$repo" \ - --arg tag "$tag" \ - --arg rev "$rev" \ - --arg url "$url" \ - --arg branch "$branch" \ - --arg sha "$sha" \ - "$F" > $file diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..1baee1f --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,82 @@ +{ pkgs }: +with pkgs; +rec { + yarn2nixPackage = let + src = builtins.fetchGit { + url = "git://github.com/moretea/yarn2nix.git"; + ref = "master"; + rev = "780e33a07fd821e09ab5b05223ddb4ca15ac663f"; + }; + in + (callPackage src {}) // { inherit src; }; + + nodeEnv = import ./node-env.nix; + + fetchedGithub = path: + let + json = lib.importJSON path; + in rec { + version = json.tag; + name = "${json.meta.name}-${version}"; + src = fetchFromGitHub json.github; + }; + + fetchedGit = path: + let + json = lib.importJSON path; + in rec { + version = json.tag; + name = "${json.meta.name}-${version}"; + src = fetchgit json.git; + }; + + fetchedGitPrivate = path: + let + json = lib.importJSON path; + in rec { + version = json.tag; + name = "${json.meta.name}-${version}"; + src = builtins.fetchGit { + url = json.git.url; + ref = "master"; + rev = json.git.rev; + }; + }; + + wrap = { paths ? [], vars ? {}, file ? null, script ? null, name ? "wrap" }: + assert file != null || script != null || + abort "wrap needs 'file' or 'script' argument"; + with rec { + set = n: v: "--set ${pkgs.lib.escapeShellArg n} " + + "${pkgs.lib.escapeShellArg v}"; + args = (map (p: "--prefix PATH : ${p}/bin") paths) ++ + (builtins.attrValues (pkgs.lib.mapAttrs set vars)); + }; + runCommand name + { + f = if file == null then writeScript name script else file; + buildInputs = [ makeWrapper ]; + } + '' + makeWrapper "$f" "$out" ${toString args} + ''; + + # This adds header colors to the builds, but it rebuilds the whole + # world from scratch, so only use it to debug! + # add it as postHook in derivations + immaePostHook = '' + header() { + echo -ne "\033[1;36m" + echo -n "$1" + echo -e "\033[0m" + } + + echoCmd() { + printf "\033[1;34m%s:\033[0m" "$1" + shift + printf ' %q' "$@" + echo + } + ''; + +} // (if builtins.pathExists ./lib/private then import ./lib/private else {}) diff --git a/libs.nix b/libs.nix deleted file mode 100644 index 48787af..0000000 --- a/libs.nix +++ /dev/null @@ -1,82 +0,0 @@ -{ pkgs }: -with pkgs; -rec { - yarn2nixPackage = let - src = builtins.fetchGit { - url = "git://github.com/moretea/yarn2nix.git"; - ref = "master"; - rev = "780e33a07fd821e09ab5b05223ddb4ca15ac663f"; - }; - in - (callPackage src {}) // { inherit src; }; - - nodeEnv = import ./lib/node-env.nix; - - fetchedGithub = path: - let - json = lib.importJSON path; - in rec { - version = json.tag; - name = "${json.meta.name}-${version}"; - src = fetchFromGitHub json.github; - }; - - fetchedGit = path: - let - json = lib.importJSON path; - in rec { - version = json.tag; - name = "${json.meta.name}-${version}"; - src = fetchgit json.git; - }; - - fetchedGitPrivate = path: - let - json = lib.importJSON path; - in rec { - version = json.tag; - name = "${json.meta.name}-${version}"; - src = builtins.fetchGit { - url = json.git.url; - ref = "master"; - rev = json.git.rev; - }; - }; - - wrap = { paths ? [], vars ? {}, file ? null, script ? null, name ? "wrap" }: - assert file != null || script != null || - abort "wrap needs 'file' or 'script' argument"; - with rec { - set = n: v: "--set ${pkgs.lib.escapeShellArg n} " + - "${pkgs.lib.escapeShellArg v}"; - args = (map (p: "--prefix PATH : ${p}/bin") paths) ++ - (builtins.attrValues (pkgs.lib.mapAttrs set vars)); - }; - runCommand name - { - f = if file == null then writeScript name script else file; - buildInputs = [ makeWrapper ]; - } - '' - makeWrapper "$f" "$out" ${toString args} - ''; - - # This adds header colors to the builds, but it rebuilds the whole - # world from scratch, so only use it to debug! - # add it as postHook in derivations - immaePostHook = '' - header() { - echo -ne "\033[1;36m" - echo -n "$1" - echo -e "\033[0m" - } - - echoCmd() { - printf "\033[1;34m%s:\033[0m" "$1" - shift - printf ' %q' "$@" - echo - } - ''; - -} // (if builtins.pathExists ./lib/private then import ./lib/private else {}) diff --git a/nix_path_env b/nix_path_env deleted file mode 100644 index 19f7cca..0000000 --- a/nix_path_env +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -if [ -z "$NIXOPS_DEPLOYMENT" ]; then - # This will automatically upgrade to latest version at each build - nixpkgs="https://nixos.org/channels/nixos-19.03/nixexprs.tar.xz" -else - nixpkgs="https://releases.nixos.org/nixos/19.03/nixos-19.03.172530.096e2f137b6/nixexprs.tar.xz" -fi -nixpkgsPrevious="$nixpkgs" -nixpkgsNext="$nixpkgs" -export NIX_PATH="nixpkgs=$nixpkgs:nixpkgsNext=$nixpkgsNext:nixpkgsPrevious=$nixpkgsPrevious" - -nixops_custom () { - DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - d=$(nix-build --no-out-link -E "with import { overlays = builtins.attrValues (import $DIR/overlays); }; nixops") - ${d}/bin/nixops "$@" -} diff --git a/nixops/default.nix b/nixops/default.nix new file mode 100644 index 0000000..649e431 --- /dev/null +++ b/nixops/default.nix @@ -0,0 +1,9 @@ +{ privateFiles ? ./. }: +{ + network = { + description = "Immae's network"; + enableRollback = true; + }; + + eldiron = import ../modules/private/system/eldiron.nix { inherit privateFiles; }; +} diff --git a/nixops/eldiron.nix b/nixops/eldiron.nix deleted file mode 100644 index 649e431..0000000 --- a/nixops/eldiron.nix +++ /dev/null @@ -1,9 +0,0 @@ -{ privateFiles ? ./. }: -{ - network = { - description = "Immae's network"; - enableRollback = true; - }; - - eldiron = import ../modules/private/system/eldiron.nix { inherit privateFiles; }; -} diff --git a/nixops/scripts/nixops_wrap b/nixops/scripts/nixops_wrap index e75fa2f..28aa917 100755 --- a/nixops/scripts/nixops_wrap +++ b/nixops/scripts/nixops_wrap @@ -8,7 +8,7 @@ fi DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops" export NIXOPS_DEPLOYMENT="$DeploymentUuid" -source $(dirname $(dirname $DIR))/nix_path_env +source $(dirname $(dirname $DIR))/scripts/nix_env TEMP=$(mktemp -d /tmp/XXXXXX-nixops-files) chmod go-rwx $TEMP diff --git a/nixops/scripts/pull_deployment b/nixops/scripts/pull_deployment index ef0f221..2f50875 100755 --- a/nixops/scripts/pull_deployment +++ b/nixops/scripts/pull_deployment @@ -8,7 +8,7 @@ fi DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops" export NIXOPS_DEPLOYMENT="$DeploymentUuid" -source $(dirname $(dirname $DIR))/nix_path_env +source $(dirname $(dirname $DIR))/scripts/nix_env export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops" diff --git a/nixops/scripts/push_deployment b/nixops/scripts/push_deployment index 26f8cd2..f2d25f9 100755 --- a/nixops/scripts/push_deployment +++ b/nixops/scripts/push_deployment @@ -8,6 +8,6 @@ fi DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops" export NIXOPS_DEPLOYMENT="$DeploymentUuid" -source $(dirname $(dirname $DIR))/nix_path_env +source $(dirname $(dirname $DIR))/scripts/nix_env nixops_custom export | pass insert -m $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/Deployment diff --git a/nixops/scripts/setup b/nixops/scripts/setup index 72b19c0..cc03482 100755 --- a/nixops/scripts/setup +++ b/nixops/scripts/setup @@ -94,7 +94,7 @@ if nix show-config --json | jq -e '.sandbox.value == "true"' >/dev/null; then fi DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -source $(dirname $(dirname $DIR))/nix_path_env +source $(dirname $(dirname $DIR))/scripts/nix_env export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops" export NIXOPS_DEPLOYMENT="$DeploymentUuid" @@ -107,14 +107,14 @@ if ! nixops_custom info 2>/dev/null >/dev/null; then if [ "$y" = "y" -o "$y" = "Y" ]; then deployment=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/Deployment) echo "$deployment" | nixops_custom import - - nixops_custom modify "$(dirname $DIR)/eldiron.nix" else echo "Aborting" exit 1 fi fi +nixops_custom modify "$(dirname $DIR)/default.nix" + cat <<-EOF All set up. Please make sure you’re using scripts/nixops_wrap when deploying diff --git a/overlays/default.nix b/overlays/default.nix index 408515e..42c0386 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -1,5 +1,5 @@ { - mylibs = self: super: { mylibs = import ../libs.nix { pkgs = self; }; }; + mylibs = self: super: { mylibs = import ../lib { pkgs = self; }; }; mypkgs = self: super: import ../pkgs { pkgs = self; }; bitlbee = import ./bitlbee; diff --git a/pkgs/default.nix b/pkgs/default.nix index 8ea65a5..297f524 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -1,7 +1,7 @@ { pkgs }: with pkgs; let - mylibs = import ../libs.nix { inherit pkgs; }; + mylibs = import ../lib { inherit pkgs; }; in rec { boinctui = callPackage ../pkgs/boinctui {}; diff --git a/pkgs/private/default.nix b/pkgs/private/default.nix index 60fabad..1abdd29 100644 --- a/pkgs/private/default.nix +++ b/pkgs/private/default.nix @@ -1,7 +1,7 @@ { pkgs }: with pkgs; let - mylibs = import ../../libs.nix { inherit pkgs; }; + mylibs = import ../../lib { inherit pkgs; }; in rec { webapps = callPackage ./webapps { diff --git a/scripts/fetch_version b/scripts/fetch_version new file mode 100755 index 0000000..a0325f6 --- /dev/null +++ b/scripts/fetch_version @@ -0,0 +1,173 @@ +#!/bin/bash + +usage() { + echo "$0 file.json" + echo "$0 [-n|--name name] [-b|--branch branch_or_rev] [-f|--file out_file] [-h|--help] (-u|--url) url" + exit +} + +branch="master" +while [[ $# -gt 0 ]]; do + a="$1" + shift + case "$a" in + *.json) + file=$a + content=$(cat $a) + name="$(echo "$content" | jq -r ".meta.name")" + url="$(echo "$content" | jq -r ".meta.url")" + branch="$(echo "$content" | jq -r ".meta.branch")" + ;; + -n|--name) + name=$1 + shift + ;; + -u|--url) + url=$1 + shift + ;; + -b|--branch) + branch=$1 + shift + ;; + -f|--file) + file=$1 + shift + ;; + -h|--help) + usage + ;; + esac +done +if [ -z "$url" ]; then + usage +fi +if [ -z "$name" ]; then + name=$(echo "$url" | cut -d"/" -f5) +fi +if [ -z "$file" ]; then + file=$name.json +fi + +# function fetch_ledger () { +# pushd $HOME/projets/ledger >/dev/null 2>/dev/null +# git fetch origin +# tag="$(git describe origin/next | sed -e "s/^v//")" +# rev="$(git show-ref -s refs/remotes/origin/next)" +# sha="$(nix-prefetch-url --unpack file://<(git archive --format=tar.gz HEAD) 2>/dev/null)" +# popd >/dev/null 2>/dev/null +# } + +# awk_describe='BEGIN { +# FS = "[ /^]+" +# while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) { +# if (!sha) +# sha = substr($0, 1, 7) +# tag = $3 +# } +# while ("curl -s " ARGV[1] "/releases/tag/" tag | getline) +# if ($3 ~ "commits") +# com = $2 +# printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha +# }' + +function get_ref () { + case "$1" in + refs/*) + echo "$1" + ;; + *) + echo "refs/heads/$1" + ;; + esac +} + +function get_name () { + branch="$1" + rev="$2" + minirev=${rev:0:7} + + case "$branch" in + refs/tags/*) + b="${branch#refs/tags/}" + echo "${b//\//-}" + ;; + refs/heads/*) + b=${branch#refs/heads/} + echo "$minirev-${b//\//-}" + ;; + refs/*) + b=${branch#refs/} + echo "$minirev-${b//\//-}" + ;; + *) + echo "$minirev-${branch//\//-}" + ;; + esac +} + +function fetch_github () { + rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)" + sha="$(nix-prefetch-url --unpack $url/archive/$rev.tar.gz)" + # Différent du git-describe et github-spécifique + #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//") + tag=$(get_name $branch $rev) +} + +function fetch_other () { + rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)" + sha="$(nix-prefetch-git --url $url --rev $(get_ref $branch) | jq -r '.sha256')" + tag=$(get_name $branch $rev) +} + +case "$url" in + https://*github.com/*) + fetch_github 2>/dev/null + owner=$(echo "$url" | cut -d"/" -f4) + repo=$(echo "$url" | cut -d"/" -f5) + + F='{ + "tag": $tag, + "meta": { + "name": $name, + "url": $url, + "branch": $branch + }, + "github": { + "owner": $owner, + "repo": $repo, + "rev": $rev, + "sha256": $sha, + "fetchSubmodules": true + } + }' + ;; + *) + fetch_other 2>/dev/null + F='{ + "tag": $tag, + "meta": { + "name": $name, + "url": $url, + "branch": $branch + }, + "git": { + "url": $url, + "rev": $rev, + "sha256": $sha, + "fetchSubmodules": true + } + }' + ;; +esac + +jq -n \ + --arg name "$name" \ + --arg owner "$owner" \ + --arg repo "$repo" \ + --arg tag "$tag" \ + --arg rev "$rev" \ + --arg url "$url" \ + --arg branch "$branch" \ + --arg sha "$sha" \ + "$F" > $file diff --git a/scripts/make-env b/scripts/make-env index ebb1eed..983a359 100755 --- a/scripts/make-env +++ b/scripts/make-env @@ -2,10 +2,11 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -source $(dirname $DIR)/nix_path_env +source $DIR/nix_env nix-env -r -i -A myEnvironments.immae-eu -f "" "$@" +result=$? cat >> $(dirname $DIR)/versions_log </dev/null 2>&1 && pwd )" + d=$(nix-build --no-out-link -E "with import { overlays = builtins.attrValues (import $(dirname $DIR)/overlays); }; nixops") + ${d}/bin/nixops "$@" +} diff --git a/scripts/nix_infos b/scripts/nix_infos index f824305..56e5b19 100755 --- a/scripts/nix_infos +++ b/scripts/nix_infos @@ -2,7 +2,7 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -source $(dirname $DIR)/nix_path_env +source $DIR/nix_env version=$(nix eval --raw nixpkgs.lib.version) mainversion=$(echo $version | cut -d"." -f -2) -- cgit v1.2.3