(基础安装的目录结构 和最终配置文件 )nixos 从0实现全集 梦中情os

目录结构

1
2
3
4
5
6
7
 ls -lh /etc/nixos/
total 20K
-rw-r--r-- 1 root root 2.3K Mar 12 19:45 configuration.nix
-rw-r--r-- 1 root root 1.2K Mar 12 20:21 flake.lock
-rw-r--r-- 1 root root 1.3K Mar 12 20:21 flake.nix
-rw-r--r-- 1 root root 1.3K Mar 12 16:31 hardware-configuration.nix
-rw-r--r-- 1 root root  669 Mar 12 20:20 home.nix

configuration.nix

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{ config, lib, pkgs, ... }:
{
  imports =
    [ 
      ./hardware-configuration.nix
    ];
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
  nix.settings.substituters = [ "https://mirror.sjtu.edu.cn/nix-channels/store" ];
  nixpkgs.config.allowUnfree = true;
  networking.hostName = "vm-nixos"; # Define your hostname.
  nix.settings.experimental-features = [ "nix-command" "flakes" ];
  time.timeZone = "Asia/Shanghai";
  virtualisation.vmware.guest.enable = true;
  sound.enable = true;
  users.mutableUsers = false; # 禁止useradd添加用户
  #security.sudo.wheelNeedsPassword = false;
  users.users.yh= { #用户名是yh
      isNormalUser = true;
      hashedPassword = "XXXXXXXXXXXXXXXX";
      extraGroups = [
        "wheel"
        "users"
      ];
    };

  environment.systemPackages = with pkgs; [
      wget
      curl
      unzip
      fish
    ];
 services.openssh.enable = true;
 users.users.root.openssh.authorizedKeys.keys = [
  "ssh-rsa XXXX joyanhui@qq.com"
];
  users.users.yh.openssh.authorizedKeys.keys = [
  "ssh-rsa XXXX joyanhui@qq.com"
  ];
  system.stateVersion = "23.11"; 
}

flake.nix

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
  description = "NixOS configuration";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
    home-manager = {
      url = "github:nix-community/home-manager/release-23.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs@{ nixpkgs, home-manager, ... }: {
    nixosConfigurations = {
      # 这里的 vm-nixos 替换成你的主机名称
      vm-nixos = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix

          # 将 home-manager 配置为 nixos 的一个 module
          # 这样在 nixos-rebuild switch 时,home-manager 配置也会被自动部署
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;

            # 这里的 ryan 也得替换成你的用户名
            # 这里的 import 函数在前面 Nix 语法中介绍过了,不再赘述
            home-manager.users.yh = import ./home.nix;

            # 取消注释下面这一行,就可以在 home.nix 中使用 flake 的所有 inputs 参数了
            # home-manager.extraSpecialArgs = inputs;
          }
        ];
      };
    };
  };
}

home.nix

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{ config, pkgs, ... }:

{
  # 注意修改这里的用户名与用户目录
  home.username = "yh";
  home.homeDirectory = "/home/yh";
  # 也可以在这里ln文件到用户目录,或者直接text写文件到用户目录
  # 通过 home.packages 安装一些常用的软件
  # 这些软件将仅在当前用户下可用,不会影响系统级别的配置
  # 建议将所有 GUI 软件,以及与 OS 关系不大的 CLI 软件,都通过 home.packages 安装 这里就安装了一个 neofetch
  home.packages = with pkgs;[
    neofetch
  ];

  home.stateVersion = "23.11";
  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;
}

hardware-configuration.nix

一般不用管,你可以修改成by-lable

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{ config, lib, pkgs, modulesPath, ... }:

{
  imports = [ ];

  boot.initrd.availableKernelModules = [ "ata_piix" "uhci_hcd" "ehci_pci" "ahci" "nvme" "sr_mod" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/e762404a-c507-44c8-914d-936a37170a89";
      fsType = "btrfs";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/2C95-235F";
      fsType = "vfat";
    };

  swapDevices =
    [ { device = "/dev/disk/by-uuid/374a6b36-390c-45f0-8e00-22ef4389dd1c"; }
    ];

  # Enables DHCP on each ethernet and wireless interface. In case of scripted networking
  # (the default) this is the recommended approach. When using systemd-networkd it's
  # still possible to use this option, but it's recommended to use it in conjunction
  # with explicit per-interface declarations with `networking.interfaces.<interface>.useDHCP`.
  networking.useDHCP = lib.mkDefault true;
  # networking.interfaces.ens160.useDHCP = lib.mkDefault true;

  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
}

flake.lock

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{
  "nodes": {
    "home-manager": {
      "inputs": {
        "nixpkgs": [
          "nixpkgs"
        ]
      },
      "locked": {
        "lastModified": 1706981411,
        "narHash": "sha256-cLbLPTL1CDmETVh4p0nQtvoF+FSEjsnJTFpTxhXywhQ=",
        "owner": "nix-community",
        "repo": "home-manager",
        "rev": "652fda4ca6dafeb090943422c34ae9145787af37",
        "type": "github"
      },
      "original": {
        "owner": "nix-community",
        "ref": "release-23.11",
        "repo": "home-manager",
        "type": "github"
      }
    },
    "nixpkgs": {
      "locked": {
        "lastModified": 1710162809,
        "narHash": "sha256-i2R2bcnQp+85de67yjgZVvJhd6rRnJbSYNpGmB6Leb8=",
        "owner": "nixos",
        "repo": "nixpkgs",
        "rev": "ddcd7598b2184008c97e6c9c6a21c5f37590b8d2",
        "type": "github"
      },
      "original": {
        "owner": "nixos",
        "ref": "nixos-23.11",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "home-manager": "home-manager",
        "nixpkgs": "nixpkgs"
      }
    }
  },
  "root": "root",
  "version": 7
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计