From df5ce785194d91d30900f47d973383a1b8d6fe63 Mon Sep 17 00:00:00 2001
From: Matei Adriel <rafaeladriel11@gmail.com>
Date: Wed, 8 Apr 2020 19:36:58 +0300
Subject: [PATCH] feat: initial config

---
 default.nix                          |  18 ++++
 modules/applications/default.nix     |   1 +
 modules/applications/git/aliases.nix |  39 ++++++++
 modules/applications/git/default.nix |  20 ++++
 modules/applications/locale.nix      |   9 ++
 modules/applications/misc.nix        |  11 +++
 modules/default.nix                  |  10 ++
 modules/dev/default.nix              |   1 +
 modules/dev/nix.nix                  |   3 +
 modules/network.nix                  |  13 +++
 modules/overlay.nix                  |  28 ++++++
 modules/users.nix                    |  11 +++
 modules/xserver.nix                  |  23 +++++
 nix/sources.json                     |  86 +++++++++++++++++
 nix/sources.nix                      | 138 +++++++++++++++++++++++++++
 options.nix                          |   1 +
 16 files changed, 412 insertions(+)
 create mode 100644 default.nix
 create mode 100644 modules/applications/default.nix
 create mode 100644 modules/applications/git/aliases.nix
 create mode 100644 modules/applications/git/default.nix
 create mode 100644 modules/applications/locale.nix
 create mode 100644 modules/applications/misc.nix
 create mode 100644 modules/default.nix
 create mode 100644 modules/dev/default.nix
 create mode 100644 modules/dev/nix.nix
 create mode 100644 modules/network.nix
 create mode 100644 modules/overlay.nix
 create mode 100644 modules/users.nix
 create mode 100644 modules/xserver.nix
 create mode 100644 nix/sources.json
 create mode 100644 nix/sources.nix
 create mode 100644 options.nix

diff --git a/default.nix b/default.nix
new file mode 100644
index 0000000..fe2badc
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,18 @@
+{ ... }:
+let sources = import ./nix/sources.nix;
+in {
+  imports = [ "${sources.home-manager}/nixos" ./modules ];
+
+  boot.loader.systemd-boot.enable = true;
+  boot.loader.efi.canTouchEfiVariables = true;
+
+  # Enable the OpenSSH daemon.
+  services.openssh.enable = true;
+
+  # Enable sound.
+  sound.enable = true;
+  hardware.pulseaudio.enable = true;
+
+  system.stateVersion = "19.09";
+}
+
diff --git a/modules/applications/default.nix b/modules/applications/default.nix
new file mode 100644
index 0000000..f336af7
--- /dev/null
+++ b/modules/applications/default.nix
@@ -0,0 +1 @@
+{ ... }: { imports = [ ./misc.nix ./locale.nix ./git ]; }
diff --git a/modules/applications/git/aliases.nix b/modules/applications/git/aliases.nix
new file mode 100644
index 0000000..7d6b587
--- /dev/null
+++ b/modules/applications/git/aliases.nix
@@ -0,0 +1,39 @@
+# Most of these are copied from: https://github.com/Brettm12345/nixos-config/blob/1400c71bce/modules/applications/git/aliases.nix
+
+let
+  git = text: ''"!git ${text}"'';
+  f = text: ''"!f(){ ${text} };f"'';
+in {
+  # Unstage all changes
+  unstage = "reset HEAD --";
+
+  # Ammend to the last commit
+  amend = "commit --amend -C HEAD";
+
+  # List branches sorted by last modified
+  b = git
+    "for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'";
+
+  # Test merge for conflicts before merging
+  mergetest = f ''
+    git merge --no-commit --no-ff "$1"; git merge --abort; echo "Merge aborted";'';
+
+  # Get description of current repo
+  description = git
+    ''config --get "branch.$(git rev-parse --abbrev-ref HEAD).description"'';
+
+  # Show authors
+  authors = ''
+    "!f() { git log --no-merges --pretty='format:%<(26)%an <%ae>' --author "$*" | sort | uniq# }# f"'';
+
+  a = "add";
+  cm = "commit --message";
+  caa = "commit --ammend";
+  cl = "clone";
+  co = "checkout";
+  col = "checkout @{-1}";
+  cob = "checkout -b";
+  st = "stash";
+  pop = "stash pop";
+  t = "tag";
+}
diff --git a/modules/applications/git/default.nix b/modules/applications/git/default.nix
new file mode 100644
index 0000000..5cd3be2
--- /dev/null
+++ b/modules/applications/git/default.nix
@@ -0,0 +1,20 @@
+{ pkgs, ... }: {
+  home-manager.users.adrielus = {
+    home.packages = with pkgs.gitAndTools; [ hub ];
+    programs.git = {
+      enable = true;
+
+      userName = "Matei Adriel";
+      userEmail = "rafaeladriel11@gmail.com";
+
+      aliases = import ./aliases.nix;
+
+      extraConfig = {
+        github.user = "Mateiadrielrafael";
+        hub.protocol = "ssh";
+
+        rebase.autoStash = true;
+      };
+    };
+  };
+}
diff --git a/modules/applications/locale.nix b/modules/applications/locale.nix
new file mode 100644
index 0000000..e82619f
--- /dev/null
+++ b/modules/applications/locale.nix
@@ -0,0 +1,9 @@
+{ ... }: {
+  i18n = {
+    consoleFont = "Lat2-Terminus16";
+    consoleKeyMap = "us";
+    defaultLocale = "en_US.UTF-8";
+  };
+
+  time.timeZone = "Europe/Bucharest";
+}
diff --git a/modules/applications/misc.nix b/modules/applications/misc.nix
new file mode 100644
index 0000000..26b7db5
--- /dev/null
+++ b/modules/applications/misc.nix
@@ -0,0 +1,11 @@
+{ pkgs, ... }: {
+  environment.systemPackages = with pkgs; [
+    # Tool to allow hashing passwords from the cmd
+    mkpasswd
+
+    vscodium
+    google-chrome
+    discord
+    git
+  ];
+}
diff --git a/modules/default.nix b/modules/default.nix
new file mode 100644
index 0000000..1b0f0df
--- /dev/null
+++ b/modules/default.nix
@@ -0,0 +1,10 @@
+{ ... }: {
+  imports = [
+    ./dev
+    ./applications
+    ./network.nix
+    ./xserver.nix
+    ./users.nix
+    ./overlay.nix
+  ];
+}
diff --git a/modules/dev/default.nix b/modules/dev/default.nix
new file mode 100644
index 0000000..bed2c28
--- /dev/null
+++ b/modules/dev/default.nix
@@ -0,0 +1 @@
+{ ... }: { imports = [ ./nix.nix ]; }
diff --git a/modules/dev/nix.nix b/modules/dev/nix.nix
new file mode 100644
index 0000000..6578ed2
--- /dev/null
+++ b/modules/dev/nix.nix
@@ -0,0 +1,3 @@
+{ pkgs, ... }: {
+  environment.systemPackages = with pkgs; [ nixfmt niv cached-nix-shell ];
+}
diff --git a/modules/network.nix b/modules/network.nix
new file mode 100644
index 0000000..6de5058
--- /dev/null
+++ b/modules/network.nix
@@ -0,0 +1,13 @@
+{ ... }: {
+  networking = {
+    networkmanager.enable = true;
+    hostName = "nixos";
+
+    # The global useDHCP flag is deprecated, therefore explicitly set to false here.
+    # Per-interface useDHCP will be mandatory in the future, so this generated config
+    # replicates the default behaviour.
+    useDHCP = false;
+    interfaces.enp1s0.useDHCP = true;
+    interfaces.wlp0s20f3.useDHCP = true;
+  };
+}
diff --git a/modules/overlay.nix b/modules/overlay.nix
new file mode 100644
index 0000000..094a18d
--- /dev/null
+++ b/modules/overlay.nix
@@ -0,0 +1,28 @@
+{ ... }:
+let imports = import ../nix/sources.nix;
+in {
+  nixpkgs.overlays = [
+    (self: super:
+      with self; rec {
+        inherit imports;
+        cached-nix-shell = callPackage imports.cached-nix-shell { };
+        inherit (import imports.niv { }) niv;
+        inherit (import imports.all-hies { }) all-hies;
+      })
+  ];
+
+  # allow packages with unfree licenses
+  nixpkgs.config.allowUnfree = true;
+
+  nix = {
+    trustedUsers = [ "root" "adrielus" "@wheel" ];
+    autoOptimiseStore = true;
+    gc.automatic = true;
+    optimise.automatic = true;
+    binaryCaches = [ "https://cache.nixos.org" "https://all-hies.cachix.org" ];
+    binaryCachePublicKeys = [
+      "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
+      "all-hies.cachix.org-1:JjrzAOEUsD9ZMt8fdFbzo3jNAyEWlPAwdVuHw4RD43k="
+    ];
+  };
+}
diff --git a/modules/users.nix b/modules/users.nix
new file mode 100644
index 0000000..37b41ab
--- /dev/null
+++ b/modules/users.nix
@@ -0,0 +1,11 @@
+{ ... }: {
+  users = {
+    mutableUsers = false;
+    users.adrielus = {
+      isNormalUser = true;
+      extraGroups = [ "wheel" "networkmanager" ];
+      hashedPassword =
+        "$6$5NX9cuUbX$yjiBbroplRLanLfJ5wNjjsd9rSvN81BCNEnuF2DUgfMa/TPYdl5PUYcWF52VxNbisDPsR2Q5EhgNrgALatpT3/";
+    };
+  };
+}
diff --git a/modules/xserver.nix b/modules/xserver.nix
new file mode 100644
index 0000000..7841d44
--- /dev/null
+++ b/modules/xserver.nix
@@ -0,0 +1,23 @@
+{ ... }: {
+  services.xserver = {
+    # Enable the X11 windowing system.
+    enable = true;
+    layout = "us";
+    xkbOptions = "eurosign:e";
+
+    # Enable the KDE Desktop Environment.
+    displayManager.sddm.enable = true;
+    desktopManager.plasma5.enable = true;
+
+    libinput = {
+      # Enable touchpad support.
+      enable = true;
+
+      naturalScrolling = true;
+      accelSpeed = "2.5";
+
+      # who thought letting this be true by default was a good idea
+      tappingDragLock = false;
+    };
+  };
+}
diff --git a/nix/sources.json b/nix/sources.json
new file mode 100644
index 0000000..41df02d
--- /dev/null
+++ b/nix/sources.json
@@ -0,0 +1,86 @@
+{
+    "NUR": {
+        "branch": "master",
+        "description": "Nix User Repository: User contributed nix packages [maintainer=@Mic92]",
+        "homepage": "",
+        "owner": "nix-community",
+        "repo": "NUR",
+        "rev": "d28e8f7d40e2683d57589f8ffa7568bdf89d316f",
+        "sha256": "0pfafab0bfs4zcld41awamwz6wn3snw1p9h2aid6pgxkbrm9r5sx",
+        "type": "tarball",
+        "url": "https://github.com/nix-community/NUR/archive/d28e8f7d40e2683d57589f8ffa7568bdf89d316f.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "all-hies": {
+        "branch": "master",
+        "description": "Cached Haskell IDE Engine Nix builds for all GHC versions",
+        "homepage": "",
+        "owner": "Infinisil",
+        "repo": "all-hies",
+        "rev": "4b6aab017cdf96a90641dc287437685675d598da",
+        "sha256": "0ap12mbzk97zmxk42fk8vqacyvpxk29r2wrnjqpx4m2w9g7gfdya",
+        "type": "tarball",
+        "url": "https://github.com/Infinisil/all-hies/archive/4b6aab017cdf96a90641dc287437685675d598da.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "cached-nix-shell": {
+        "branch": "master",
+        "description": "Instant startup time for nix-shell",
+        "homepage": "",
+        "owner": "xzfc",
+        "repo": "cached-nix-shell",
+        "rev": "94e4eb97aa84e1e9c4ecb5ebe2b08b9da22df2a2",
+        "sha256": "0pzwknpc4qrh9pv5z0xvldql2dkj9ddksvaci86a4f8cnd86p2l6",
+        "type": "tarball",
+        "url": "https://github.com/xzfc/cached-nix-shell/archive/94e4eb97aa84e1e9c4ecb5ebe2b08b9da22df2a2.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "easy-purescript-nix": {
+        "branch": "master",
+        "description": "Easy PureScript (and other tools) with Nix",
+        "homepage": "",
+        "owner": "justinwoo",
+        "repo": "easy-purescript-nix",
+        "rev": "aa3e608608232f4a009b5c132ae763fdabfb4aba",
+        "sha256": "0y6jikncxs9l2zgngbd1775f1zy5s1hdc5rhkyzsyaalcl5cajk8",
+        "type": "tarball",
+        "url": "https://github.com/justinwoo/easy-purescript-nix/archive/aa3e608608232f4a009b5c132ae763fdabfb4aba.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "home-manager": {
+        "branch": "release-19.09",
+        "description": "Manage a user environment using Nix",
+        "homepage": "https://rycee.gitlab.io/home-manager/",
+        "owner": "rycee",
+        "repo": "home-manager",
+        "rev": "0d1ca254d0f213a118459c5be8ae465018132f74",
+        "sha256": "0sw8lw825gg04h6js42bvackgydi5m0xsjvnb5gxlqv45qw8rxjq",
+        "type": "tarball",
+        "url": "https://github.com/rycee/home-manager/archive/0d1ca254d0f213a118459c5be8ae465018132f74.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "niv": {
+        "branch": "master",
+        "description": "Easy dependency management for Nix projects",
+        "homepage": "https://github.com/nmattia/niv",
+        "owner": "nmattia",
+        "repo": "niv",
+        "rev": "f73bf8d584148677b01859677a63191c31911eae",
+        "sha256": "0jlmrx633jvqrqlyhlzpvdrnim128gc81q5psz2lpp2af8p8q9qs",
+        "type": "tarball",
+        "url": "https://github.com/nmattia/niv/archive/f73bf8d584148677b01859677a63191c31911eae.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "nixpkgs": {
+        "branch": "nixos-19.09",
+        "description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
+        "homepage": "https://github.com/NixOS/nixpkgs",
+        "owner": "NixOS",
+        "repo": "nixpkgs-channels",
+        "rev": "e10c65cdb35b6a66491e47e5a85f5d456b4f4eea",
+        "sha256": "19csb2s3wyav83zcw9dw488zk2fnz6wcxxz8q6hy43dbph86hxwm",
+        "type": "tarball",
+        "url": "https://github.com/NixOS/nixpkgs-channels/archive/e10c65cdb35b6a66491e47e5a85f5d456b4f4eea.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    }
+}
diff --git a/nix/sources.nix b/nix/sources.nix
new file mode 100644
index 0000000..7bd0f3e
--- /dev/null
+++ b/nix/sources.nix
@@ -0,0 +1,138 @@
+# This file has been generated by Niv.
+
+let
+
+  #
+  # The fetchers. fetch_<type> fetches specs of type <type>.
+  #
+
+  fetch_file = pkgs: spec:
+    if spec.builtin or true then
+      builtins_fetchurl { inherit (spec) url sha256; }
+    else
+      pkgs.fetchurl { inherit (spec) url sha256; };
+
+  fetch_tarball = pkgs: name: spec:
+    let
+      ok = str: ! builtins.isNull (builtins.match "[a-zA-Z0-9+-._?=]" str);
+      # sanitize the name, though nix will still fail if name starts with period
+      name' = stringAsChars (x: if ! ok x then "-" else x) "${name}-src";
+    in
+      if spec.builtin or true then
+        builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
+      else
+        pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
+
+  fetch_git = spec:
+    builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
+
+  fetch_local = spec: spec.path;
+
+  fetch_builtin-tarball = name: throw
+    ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
+        $ niv modify ${name} -a type=tarball -a builtin=true'';
+
+  fetch_builtin-url = name: throw
+    ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
+        $ niv modify ${name} -a type=file -a builtin=true'';
+
+  #
+  # Various helpers
+  #
+
+  # The set of packages used when specs are fetched using non-builtins.
+  mkPkgs = sources:
+    let
+      sourcesNixpkgs =
+        import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
+      hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
+      hasThisAsNixpkgsPath = <nixpkgs> == ./.;
+    in
+      if builtins.hasAttr "nixpkgs" sources
+      then sourcesNixpkgs
+      else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
+        import <nixpkgs> {}
+      else
+        abort
+          ''
+            Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
+            add a package called "nixpkgs" to your sources.json.
+          '';
+
+  # The actual fetching function.
+  fetch = pkgs: name: spec:
+
+    if ! builtins.hasAttr "type" spec then
+      abort "ERROR: niv spec ${name} does not have a 'type' attribute"
+    else if spec.type == "file" then fetch_file pkgs spec
+    else if spec.type == "tarball" then fetch_tarball pkgs name spec
+    else if spec.type == "git" then fetch_git spec
+    else if spec.type == "local" then fetch_local spec
+    else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
+    else if spec.type == "builtin-url" then fetch_builtin-url name
+    else
+      abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
+
+  # Ports of functions for older nix versions
+
+  # a Nix version of mapAttrs if the built-in doesn't exist
+  mapAttrs = builtins.mapAttrs or (
+    f: set: with builtins;
+    listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
+  );
+
+  # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
+  range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);
+
+  # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
+  stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
+
+  # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
+  stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
+  concatStrings = builtins.concatStringsSep "";
+
+  # fetchTarball version that is compatible between all the versions of Nix
+  builtins_fetchTarball = { url, name, sha256 }@attrs:
+    let
+      inherit (builtins) lessThan nixVersion fetchTarball;
+    in
+      if lessThan nixVersion "1.12" then
+        fetchTarball { inherit name url; }
+      else
+        fetchTarball attrs;
+
+  # fetchurl version that is compatible between all the versions of Nix
+  builtins_fetchurl = { url, sha256 }@attrs:
+    let
+      inherit (builtins) lessThan nixVersion fetchurl;
+    in
+      if lessThan nixVersion "1.12" then
+        fetchurl { inherit url; }
+      else
+        fetchurl attrs;
+
+  # Create the final "sources" from the config
+  mkSources = config:
+    mapAttrs (
+      name: spec:
+        if builtins.hasAttr "outPath" spec
+        then abort
+          "The values in sources.json should not have an 'outPath' attribute"
+        else
+          spec // { outPath = fetch config.pkgs name spec; }
+    ) config.sources;
+
+  # The "config" used by the fetchers
+  mkConfig =
+    { sourcesFile ? ./sources.json
+    , sources ? builtins.fromJSON (builtins.readFile sourcesFile)
+    , pkgs ? mkPkgs sources
+    }: rec {
+      # The sources, i.e. the attribute set of spec name to spec
+      inherit sources;
+
+      # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
+      inherit pkgs;
+    };
+in
+mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }
diff --git a/options.nix b/options.nix
new file mode 100644
index 0000000..7086657
--- /dev/null
+++ b/options.nix
@@ -0,0 +1 @@
+{ ... }: { imports = [ <home-manager/nixos> ]; }