├── .gitignore ├── .github └── workflows │ └── build.yml ├── flake.nix ├── flake.lock ├── nm2nix.py ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /result 2 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: "Build" 2 | on: 3 | pull_request: 4 | push: 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v3 10 | - uses: cachix/install-nix-action@v22 11 | with: 12 | github_access_token: ${{ secrets.GITHUB_TOKEN }} 13 | - run: nix build 14 | - run: nix flake check --all-systems 15 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | inputs = { 3 | nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; 4 | }; 5 | outputs = { self, nixpkgs }: 6 | { 7 | packages.x86_64-linux.default = nixpkgs.legacyPackages.x86_64-linux.writers.writePython3Bin "nm2nix" { } (builtins.readFile ./nm2nix.py); 8 | packages.aarch64-linux.default = nixpkgs.legacyPackages.aarch64-linux.writers.writePython3Bin "nm2nix" { } (builtins.readFile ./nm2nix.py); 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "nixpkgs": { 4 | "locked": { 5 | "lastModified": 1706371002, 6 | "narHash": "sha256-dwuorKimqSYgyu8Cw6ncKhyQjUDOyuXoxDTVmAXq88s=", 7 | "owner": "nixos", 8 | "repo": "nixpkgs", 9 | "rev": "c002c6aa977ad22c60398daaa9be52f2203d0006", 10 | "type": "github" 11 | }, 12 | "original": { 13 | "owner": "nixos", 14 | "ref": "nixos-unstable", 15 | "repo": "nixpkgs", 16 | "type": "github" 17 | } 18 | }, 19 | "root": { 20 | "inputs": { 21 | "nixpkgs": "nixpkgs" 22 | } 23 | } 24 | }, 25 | "root": "root", 26 | "version": 7 27 | } 28 | -------------------------------------------------------------------------------- /nm2nix.py: -------------------------------------------------------------------------------- 1 | from os import listdir 2 | from subprocess import check_output 3 | from os.path import isfile, join 4 | import configparser 5 | import tempfile 6 | import json 7 | 8 | path = "./" 9 | 10 | files = [f for f in listdir(path) if isfile(join(path, f))] 11 | nmfiles = [f for f in files if f.endswith(".nmconnection")] 12 | 13 | jsonConfigs = {} 14 | 15 | for i in nmfiles: 16 | config = configparser.ConfigParser(delimiters=('=', ), interpolation=None) 17 | config.read(i) 18 | connection_name = i.removesuffix(".nmconnection") 19 | jsonConfigs[connection_name] = {} 20 | for section in config.sections(): 21 | jsonConfigs[connection_name][section] = {} 22 | for key in config[section]: 23 | jsonConfigs[connection_name][section][key] = config[section][key] 24 | 25 | with tempfile.NamedTemporaryFile(mode="w") as tf: 26 | tf.write(json.dumps(jsonConfigs)) 27 | tf.flush() 28 | print(check_output(["nix-instantiate", "--expr", "--eval", f"builtins.fromJSON (builtins.readFile \"{tf.name}\")"], text=True)) # noqa: E501 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Dracula Theme 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NetworkManager to Nix 2 | 3 | This is a dumb script that converts every .nmconnection file in the current directory to the nix code that is expected by `networking.networkmanager.ensureProfiles.profiles` which was introduced in [NixOS/nixpkgs/#254647](https://github.com/NixOS/nixpkgs/pull/254647) 4 | You want to pipe the output of this program through some formatter, for example `nixfmt` 5 | 6 | You probably want to run this script in `/etc/NetworkManager/system-connections/` (default profile storage) or `/run/NetworkManager/system-connections` (temporary profile storage) both folders are only readable by the root user, so you need to execute the script with root permissions aka sudo. For more details about the locations feel free to read [redhat's docs](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/assembly_networkmanager-connection-profiles-in-keyfile-format_configuring-and-managing-networking) 7 | 8 | The code gets outputted as one line of nix, so you probably want to run it through a formatter like nixfmt (which the example below does). 9 | 10 | If you just want to run the script do: 11 | ```bash 12 | sudo su -c "cd /etc/NetworkManager/system-connections && nix --extra-experimental-features 'nix-command flakes' run github:Janik-Haag/nm2nix | nix --extra-experimental-features 'nix-command flakes' run nixpkgs#nixfmt-rfc-style" 13 | ``` 14 | --------------------------------------------------------------------------------