Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local path to nixpkgs to avoid constant nixpkgs download #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ You'd run:
./manage stage deploy --check # Manage script usage.
```

This assume there is a file `./stage.nixops` where this state is being stored.
This assume there is a file `./stage/stage.nixops` where this state is being stored.

Use `./manage --help` to see normal NixOps help.
Use `./manage {deployment} .shell` to open a Nix shell where the environment is set up to use
Expand Down
72 changes: 15 additions & 57 deletions manage
Original file line number Diff line number Diff line change
@@ -1,86 +1,44 @@
#!/usr/bin/env bash
#! /usr/bin/env nix-shell
#! nix-shell -I nixpkgs-overlays=./overlays
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/f7f1a2f54ef2b744a26e37418dd6354bc46aa20a.tar.gz
#! nix-shell -i bash -p nixopsUnstable3 which jq nix openssl openssh


# A NixOps Wrapper for Git Projects
# ---------------------------------
#
# Repo: https://github.com/grafted-in/nixops-manager
#
# This tool is a simple wrapper around NixOps. The goal is to make it easier to use NixOps when you
# want to share your deployment state between members of a team.
#
# To achieve this, this wrapper gives every deployment as a separate state file which is placed
# in the same directory as this script. The files have the `.nixops` extension.
#
# You are expected to keep these files in version control. It's also *highly* recommended that you
# use a tool like git-crypt to keep them encrypted with this entry in .gitattributes:
#
# *.nixops binary filter=git-crypt diff=git-crypt
#
# This tool also enforces a per-repository version of Nixpkgs via a `nixpkgs-version.sh` file in the
# same directory as the script. This ensures that all users have a consistent version of NixOps and
# deploy a consistent set of packages to servers.
#
# Most commands work identically to NixOps. However, instead of specifying deployments with
# the `--deployment/-d` flag, you select a deployment in the first argument. In other words, instead
# of the normal NixOps usage of
#
# nixops deploy -d stage --check # Normal nixops usage.
#
# You'd run:
#
# ./manage stage deploy --check # Manage script usage.
#
# This assume there is a file ./stage.nixops where this state is being stored.
# Modified from repo: https://github.com/grafted-in/nixops-manager
# This assumes there is a file ./stage/stage.nixops where this state is being stored.
#
# Use `./manage --help` to see normal NixOps help.
# Use `./manage {deployment} command` to user normal NixOps commands.
# Use `./manage {deployment} .shell` to open a Nix shell where the environment is set up to use
# `nixops` directly with the same behavior as running `./manage` commands.

set -e

# Check for Nix tools.
command -v nix-shell >/dev/null 2>&1 || {
nix_profile="$HOME/.nix-profile/etc/profile.d/nix.sh"
if [ -e "$nix_profile" ]; then
source "$nix_profile"
else
>&2 echo "Failed to find 'nix-shell' on PATH or a Nix profile to load. Have you installed Nix?"
exit 1
fi
}

here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
repo_root=$(cd "$here" && git rev-parse --show-toplevel) # Use Git to find repo root.

deployment="$1"
command="$2"
state_file="$here/${deployment}.nixops"
state_file="$here/${deployment}/${deployment}.nixops"

source "$here/nixpkgs-version.sh"
export NIX_PATH=nixpkgs-overlays=./overlays:nixpkgs=$(nix-instantiate --eval ./nixpkgs.nix | tr -d '"' ):./${deployment}

export NIX_PATH=nixpkgs="$nixpkgs_snapshot":"$repo_root":.
export NIXOPS_STATE="$state_file"
export NIXOPS_DEPLOYMENT="$deployment"

withNixops="nix-shell -p $nixops_version --run"

# Arg list trick:
# https://stackoverflow.com/questions/3104209
# ARGS=$(printf "%q"" " "$@")
export BOTO_USE_ENDPOINT_HEURISTICS=True

if [[ $deployment == --* ]]; then
ARGS=$(printf "%q"" " "$@")
$withNixops "nixops $ARGS"
nixops "$@"
exit $?
elif [ "$command" == ".shell" ]; then
nix-shell -p "$nixops_version"
exec nix-shell -p nixopsUnstable3 --command "echo You may now run commands for deployment:$deploymnet with \\'nixops\\' ; return "
elif [ ! -e "$state_file" ] && [ "$command" != "create" ]; then
>&2 echo "You're trying to use a deployment that doesn't exist yet. Try running $0 $deployment create"
exit 1
elif [ -e "$state_file" ] && [ "$command" == "create" ]; then
>&2 echo "You're trying to create a deployment that already exists."
exit 1
else
ARGS=$(printf "%q"" " "${@:2}")
$withNixops "nixops $ARGS"
fi
nixops "${@:2}"
fi
14 changes: 0 additions & 14 deletions nixpkgs-version.nix

This file was deleted.

18 changes: 0 additions & 18 deletions nixpkgs-version.sh

This file was deleted.

11 changes: 9 additions & 2 deletions nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Import this instead of <nixpkgs> to get the repo-specific version of nixpkgs.
builtins.fetchTarball {
name = "nixos-unstable-2019-03-08";
#url = https://github.com/johanot/nixpkgs/archive/b3905f0d224bc5c2da3572f7017f05a27df742cd.tar.gz;
#sha256 = "05jxiyagfgl7s061vz2xmalrrbaz95ymcxdv89j3s700hicyd8pf";
url = https://github.com/NixOS/nixpkgs/archive/f7f1a2f54ef2b744a26e37418dd6354bc46aa20a.tar.gz;
sha256 = "061sissl4kr4bzh7pl9a1vihmvf71k9iw0kblvhgvvp6pl10sv0r";
}

import ((import <nixpkgs> {}).fetchzip (import ./nixpkgs-version.nix))
# How to push a cache to arm.nixos.org
# ./manage c export | jq '.[].configsPath' -r | ( read conf ; nix-store -qR $conf | while read line; do echo `nix-store -q --size $line` $line ; done | sort -nr | head -n10 | cut -d' ' -f2 | grep -E 'linux|icu4c|glibc|systemd|glib|coreutils|initrd|util-linux' | cachix push arm )
63 changes: 63 additions & 0 deletions top-level.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

{ targetEnv ? "virtualbox" } :
let
region = "us-gov-west-1";
zone = "us-gov-west-1a";
accessKeyId = "default";
machine =
{ config, pkgs, resources, ... }:{
imports =
(if targetEnv == "ec2" then
[<nixpkgs/nixos/modules/virtualisation/amazon-image.nix>] else []) ++
[ <nixpkgs/nixos/modules/profiles/minimal.nix> ];
deployment.targetEnv = targetEnv;
deployment.ec2 = {
keyPair = resources.ec2KeyPairs.my-key-pair;
ebsInitialRootDiskSize = 30;
ebsOptimized = true;
associatePublicIpAddress = true;
ami = "ami-bb7618da";
inherit region zone accessKeyId;
instanceType = "t3.large";
};
ec2.hvm = true;
boot.initrd.postDeviceCommands= ''
${pkgs.e2fsprogs}/sbin/e2fsck -fp /dev/disk/by-label/nixos
${pkgs.e2fsprogs}/sbin/resize2fs /dev/disk/by-label/nixos
'';

deployment.virtualbox.memorySize = 4096; # megabytes
deployment.virtualbox.vcpu = 2; # number of cpus
deployment.virtualbox.headless = true;

networking.extraHosts = "${config.networking.privateIPv4} api.kube";
services.kubernetes = {
easyCerts = true;
addons.dashboard.enable = true;
roles = ["master" "node"];
apiserver = {
securePort = 443;
advertiseAddress = config.networking.privateIPv4;
};
masterAddress = "api.kube";
};
services.dockerRegistry.enable = true;
environment.systemPackages = with pkgs; [
kompose kubectl
vim e2fsprogs
];
networking.firewall.allowedTCPPorts = [22 443 ];
users.users.root.openssh.authorizedKeys = {
keyFiles = [ ];
};
};
in {
kubemaster = machine;

network.description = "kubenixops";
network.enableRollback = true;
resources = if targetEnv == "ec2" then {
ec2KeyPairs.my-key-pair = { inherit region accessKeyId; };
}else {};

}